-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_odd_even.c
More file actions
175 lines (148 loc) · 4.36 KB
/
serial_odd_even.c
File metadata and controls
175 lines (148 loc) · 4.36 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* File: serial_odd_even.c
*
* Purpose: Use odd-even transposition sort to sort a list of ints.
*
* Usage: odd_even <n> <g|i>
* n: number of elements in list
* 'g': generate list using a random number generator
* 'i': user input list
*
* Input: list (optional)
* Output: sorted list
*/
#include <stdio.h>
#include <stdlib.h>
// const int RMAX = 1000000000;
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);
void Odd_even_iter(int a[], int n, int phase);
void Swap(int* x_p, int* y_p);
/*-----------------------------------------------------------------*/
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 */
/*-----------------------------------------------------------------
* Function: Usage
* Purpose: Summary of how to run program
*/
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 */
/*-----------------------------------------------------------------
* Function: Get_args
* Purpose: Get and check command line arguments
* In args: argc, argv
* Out args: n_p, g_i_p
*/
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 */
/*-----------------------------------------------------------------
* Function: Generate_list
* Purpose: Use random number generator to generate list elements
* In args: n
* Out args: a
*/
void Generate_list(int a[], int n) {
int i;
srandom(1);
for (i = 0; i < n; i++)
a[i] = random() % RMAX;
} /* Generate_list */
/*-----------------------------------------------------------------
* Function: Print_list
* Purpose: Print the elements in the list
* In args: a, n
*/
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 */
/*-----------------------------------------------------------------
* Function: Read_list
* Purpose: Read elements of list from stdin
* In args: n
* Out args: a
*/
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 */
/*-----------------------------------------------------------------
* Function: Odd_even_sort
* Purpose: Sort list using odd-even transposition sort
* In args: n
* In/out args: a
*/
void Odd_even_sort(int a[], int n) {
int phase;
for (phase = 0; phase < n; phase++) {
Odd_even_iter(a, n, phase);
}
} /* Odd_even_sort */
/*-----------------------------------------------------------------
* Function: Odd_even_iter
* Purpose: Execute one iteration of odd-even transposition sort
* In args: n, phase
* In/out args: a
*/
void Odd_even_iter(int a[], int n, int phase) {
int i, left, right;
if (phase % 2 == 0) { /* Even phase: odd subscripts look left */
for (i = 1; i < n; i += 2) {
left = i-1;
if (a[left] > a[i]) Swap(&a[left],&a[i]);
}
} else { /* Odd phase: odd subscripts look right */
for (i = 1; i < n-1; i += 2) {
right = i+1;
if (a[i] > a[right]) Swap(&a[i], &a[right]);
}
}
} /* Odd_even_iter */
/*-----------------------------------------------------------------
* Function: Swap
* Purpose: Swap contents of x_p and y_p
* In/out args: x_p, y_p
*/
void Swap(int* x_p, int* y_p) {
int temp = *x_p;
*x_p = *y_p;
*y_p = temp;
} /* Swap */