-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_bubble.c
More file actions
155 lines (128 loc) · 3.74 KB
/
serial_bubble.c
File metadata and controls
155 lines (128 loc) · 3.74 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
/* File: serial_bubble.c
*
* Purpose: Use bubble sort to sort a list of ints.
*
* Compile: gcc -g -Wall -o serial_bubble serial_bubble.c
* Usage: serial_bubble <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>
/* For random list, 0 <= keys < RMAX */
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 Bubble_sort(int a[], int n);
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);
}
Bubble_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: Bubble_sort
* Purpose: Sort list using bubble sort
* In args: n
* In/out args: a
*/
void Bubble_sort(int a[], int n) {
int list_len, i;
for (list_len = n; list_len >= 2; list_len--)
for (i = 0; i < list_len-1; i++)
if (a[i] > a[i+1]) Swap(&a[i], &a[i+1]);
} /* Bubble_sort */
/*-----------------------------------------------------------------
* 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 */