-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenSort.cpp
More file actions
50 lines (41 loc) · 980 Bytes
/
OddEvenSort.cpp
File metadata and controls
50 lines (41 loc) · 980 Bytes
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
#include<bits/stdc++.h>
using namespace std;
void swap(int *, int *);
void oddevenSort(int *, int );
int main()
{
int a[1000], i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter elements:\n");
for (i=0; i < n; i++)
scanf("%d", &a[i]);
oddevenSort(a, n);
printf("Sorted elements are:\n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
return 0;
}
void swap(int * x, int * y) {
int temp = *x;
*x = *y;
*y = temp;
}
void oddevenSort(int *x, int n) {
int sort = 0, i;
while (!sort) {
sort = 1;
for (i=1; i<n-1; i += 2) {
if (x[i] > x[i+1]) {
swap(&x[i], &x[i+1]);
sort = 0;
}
}
for (i = 0; i<n; i += 2) {
if (x[i] > x[i + 1]) {
swap(&x[i], &x[i + 1]);
sort = 0;
}
}
}
}