-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathex13.c
60 lines (49 loc) · 1.28 KB
/
ex13.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
/*
* 13. Modify the sort() function from Program 7.12 to take a third argument
* indicating whether the array is to be sorted in ascending or descending
* order. Then modify the sort() algorithm to correctly sort the array into the
* indicated order.
*
* By Faisal Saadatmand
*/
/*
* Note: this exercise could be made more succinct and efficient by using
* features that have not been discussed at this point in the book. See
* ext13a.c for an implementation.
*/
#include <stdio.h>
#include <stdbool.h>
/* functions */
void sort(int [], int, bool);
void sort(int a[], int n, bool sordOrder)
{
int i, j, temp, cond;
for (i = 0; i < n - 1; ++i)
for (j = i + 1; j < n; ++j) {
cond = (sordOrder) ? a[i] < a[j] : a[i] > a[j];
if (cond) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
int main(void)
{
int i;
int array[16] = {34, -5, 6, 0, 12, 100, 56, 22,
44, -3, -9, 12, 17, 22, 6, 11};
printf("\nBefore sorting:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
sort(array, 16, 0);
printf("\n\nSorted in ascending order:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
sort(array, 16, 1);
printf("\n\nSorted in descending order:\n");
for (i = 0; i < 16; ++i)
printf(" %i", array[i]);
printf("\n\n");
return 0;
}