-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_odd_even_sort.c
104 lines (85 loc) · 2.37 KB
/
my_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
const int RMAX = 100;
void Usage(char* prog_name);
void Get_args(int argc, char* argv[], int* n_p, char* g_i_p);
void Generate_list(int a[], int n);
void Print_list(int a[], int n, char* title);
void Read_list(int a[], int n);
void Odd_even_sort(int a[], int n);
/*-----------------------------------------------------------------*/
int main(int argc, char* argv[]) {
int n;
char g_i;
int* a;
Get_args(argc, argv, &n, &g_i);
a = (int*) malloc(n*sizeof(int));
if (g_i == 'g') {
Generate_list(a, n);
Print_list(a, n, "Before sort");
} else {
Read_list(a, n);
}
Odd_even_sort(a, n);
Print_list(a, n, "After sort");
free(a);
return 0;
} /* main */
void Usage(char* prog_name) {
fprintf(stderr, "usage: %s <n> <g|i>\n", prog_name);
fprintf(stderr, " n: number of elements in list\n");
fprintf(stderr, " 'g': generate list using a random number generator\n");
fprintf(stderr, " 'i': user input list\n");
} /* Usage */
void Get_args(int argc, char* argv[], int* n_p, char* g_i_p) {
if (argc != 3 ) {
Usage(argv[0]);
exit(0);
}
*n_p = atoi(argv[1]);
*g_i_p = argv[2][0];
if (*n_p <= 0 || (*g_i_p != 'g' && *g_i_p != 'i') ) {
Usage(argv[0]);
exit(0);
}
} /* Get_args */
void Generate_list(int a[], int n) {
int i;
srandom(0);
for (i = 0; i < n; i++)
a[i] = random() % RMAX;
} /* Generate_list */
void Print_list(int a[], int n, char* title) {
int i;
printf("%s:\n", title);
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n\n");
} /* Print_list */
void Read_list(int a[], int n) {
int i;
printf("Please enter the elements of the list\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
} /* Read_list */
void Odd_even_sort(
int a[] /* in/out */,
int n /* in */) {
int phase, i, temp;
for (phase = 0; phase < n; phase++)
if (phase % 2 == 0) { /* Even phase */
for (i = 1; i < n; i += 2)
if (a[i-1] > a[i]) {
temp = a[i];
a[i] = a[i-1];
a[i-1] = temp;
}
} else { /* Odd phase */
for (i = 1; i < n-1; i += 2)
if (a[i] > a[i+1]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
} /* Odd_even_sort */