Implement Odd even Sort
#include
#include
#define max 7
void swap(int *,int *);
void oddeven_sort(int *);
void main()
{
int
a[max], i;
clrscr();
printf("enter the elements in to the matrix :");
for
(i = 0;i < max;i++)
{
scanf("%d", &a[i]);
}
printf("sorted elements are :\n");
oddeven_sort(a);
for
(i = 0;i < max;i++)
{
printf(" %d", a[i]);
}
}
void swap(int * x, int * y)
{
int
temp;
temp
= *x;
*x =
*y;
*y =
temp;
}
void oddeven_sort(int * x)
{
int
sort = 0, i;
while (!sort)
{
sort = 1;
for (i = 1;i < MAX;i += 2)
{
if (x[i] > x[i+1])
{
swap(&x[i], &x[i+1]);
sort = 0;
}
}
for (i = 0;i < MAX - 1;i += 2)
{
if (x[i] > x[i + 1])
{
swap(&x[i], &x[i + 1]);
sort = 0;
}
}
}
}
Output
enter the elements in
to the matrix :7 8 3 2 5 4 9
sorted elements are :
2 3 4 5 7 8 9
enter the elements in
to the matrix :1 2 3 4 5 6 7
sorted elements are :
1 2 3 4 5 6 7
enter the elements in
to the matrix :7 6 5 4 3 2 1
sorted elements are :
1 2 3 4 5 6 7
Implement Cock Tail Sort
#include
#include
#define max 8
int main()
{
int data[max];
int i, j, n, c;
clrscr();
printf("\nEnter the data");
for (i = 0; i < max; i++)
{
scanf("%d", &data[i]);
}
n = max;
do
{
for (i = 0; i < n - 1; i++)
{
if (data[i] > data[i + 1])
{
data[i] = data[i] + data[i + 1];
data[i + 1] = data[i] - data[i + 1];
data[i] = data[i] - data[i + 1];
}
}
n = n - 1;
for (i= MAX - 1, c = 0; i >= c; i--)
{
if(data[i] < data[i - 1])
{
data[i] = data[i] + data[i - 1];
data[i - 1] = data[i] - data[i - 1];
data[i] = data[i] - data[i - 1];
}
}
c = c + 1;
} while (n != 0 && c != 0);
printf("The sorted elements are:");
for (i = 0; i < max; i++)
{
printf("%d\t", data[i]);
}
getch();
}
Output
Enter the data
9 6 2 12 11 9 3 7
The sorted elements are:2 3 6 7 9 9 11 12
Enter the data
8 7 6 5 4 3 2 1
The sorted elements are:1 2 3 4 5 6 7 8
Enter the data
1 2 3 4 5 6 7 8
The sorted elements are:1 2 3 4 5 6 7 8
Implement Bitonic sort
#include
#include
#include
#define max 8
#define swap(x,y) t = x; x = y; y = t;
void compare();
void bitonicmerge(int, int, int);
void recbitonic(int, int, int);
void sort();
int data[max];
int up = 1;
int down = 0;
int main()
{
int i;
clrscr();
printf("\nEnter the data");
for (i = 0;i < max ;i++)
{
scanf("%d", &data[i]);
}
sort();
for (i = 0;i < max;i++)
{
printf("%d ", data[i]);
}
}
void compare(int i, int j, int dir)
{
int t;
if (dir == (data[i] > data[j]))
{
swap(data[i], data[j]);
}
}
void bitonicmerge(int low, int c, int dir)
{
int k, i;
if (c > 1)
{
k = c / 2;
for (i = low;i < low+k ;i++)
compare(i, i+k, dir);
bitonicmerge(low, k, dir);
bitonicmerge(low+k, k, dir);
}
}
void recbitonic(int low, int c, int dir)
{
int k;
if (c > 1)
{
k = c / 2;
recbitonic(low, k, up);
recbitonic(low + k, k, down);
bitonicmerge(low, c, dir);
}
}
void sort()
{
recbitonic(0, max, up);
}
Output
Enter the data
3 5 8 9 7 4 2 1
1 2 3 4 5 7 8 9
Enter the data
100 99 98 97 96 95 94 93
93 94 95 96 97 98 99 100
Enter the data
1111 2222 3333 4444 5555 6666 7777 8888
1111 2222 3333 4444 5555 6666 7777 8888
No comments:
Post a Comment