-
Notifications
You must be signed in to change notification settings - Fork 364
/
Odd_Even_Sort.c
59 lines (52 loc) · 1.04 KB
/
Odd_Even_Sort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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;
}
}
}
}