This C Program implement oddeven sort.
Here is source code of the C Program to implement oddeven sort. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Implement Oddeven Sort
*/
#include <stdio.h>
#define MAX 7
void swap(int *,int *);
void oddeven_sort(int *);
void main()
{
int a[MAX], i;
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]);
}
}
/* swaps the elements */
void swap(int * x, int * y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
/* sorts the array using oddeven algorithm */
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;
}
}
}
}
$ cc oddevensort.c /* average case */ $ a.out enter the elements in to the matrix :7 8 3 2 5 4 9 sorted elements are : 2 3 4 5 7 8 9 /* best case */ $ a.out enter the elements in to the matrix :1 2 3 4 5 6 7 sorted elements are : 1 2 3 4 5 6 7 /* worst case */ $ a.out enter the elements in to the matrix :7 6 5 4 3 2 1 sorted elements are : 1 2 3 4 5 6 7
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Related Posts:
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check C Books
- Check Computer Science Books
- Practice Computer Science MCQs