# C Program to Implement Odd Even Sort

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.

1. `/*`
2. ` * C Program to Implement Oddeven Sort `
3. ` */`
4. `#include <stdio.h>`
5. `#define MAX 7`
6. ` `
7. `void swap(int *,int *);`
8. `void oddeven_sort(int *);`
9. ` `
10. `void main()`
11. `{`
12. `    int a[MAX], i;`
13. ` `
14. `    printf("enter the elements in to the matrix :");`
15. `    for (i = 0;i < MAX;i++)`
16. `    {`
17. `        scanf("%d", &a[i]);`
18. `    }`
19. `    printf("sorted elements are :\n");`
20. `    oddeven_sort(a);`
21. `    for (i = 0;i < MAX;i++)`
22. `    {`
23. `        printf(" %d", a[i]);`
24. `    }`
25. `}`
26. ` `
27. `/* swaps the elements */`
28. `void swap(int * x, int * y)`
29. `{`
30. `    int temp;`
31. ` `
32. `    temp = *x;`
33. `    *x = *y;`
34. `    *y = temp; `
35. `}`
36. ` `
37. `/* sorts the array using oddeven algorithm */`
38. `void oddeven_sort(int * x)`
39. `{`
40. `    int sort = 0, i;`
41. ` `
42. `    while (!sort)`
43. `    {`
44. `        sort = 1;`
45. `        for (i = 1;i < MAX;i += 2)`
46. `        {`
47. `            if (x[i] > x[i+1])`
48. `            {`
49. `                swap(&x[i], &x[i+1]);`
50. `                sort = 0;`
51. `            }`
52. `        }`
53. `        for (i = 0;i < MAX - 1;i += 2)`
54. `        {`
55. `            if (x[i] > x[i + 1])`
56. `            {`
57. `                swap(&x[i], &x[i + 1]);`
58. `                sort = 0;`
59. `            }`
60. `        }`
61. `    }`
62. `}`

```\$ 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.

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.

If you find any mistake above, kindly email to [email protected]