-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.8.14E.c
50 lines (39 loc) · 929 Bytes
/
ex.8.14E.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
// Program to sort an array of integers into ascending order
#include <stdio.h>
#define length 16
int array[length] = {
34, -5, 6, 0, 12, 100, 56, 22,
44, -3, -9, 12, 17, 22, 6, 11
};
int ascending;
void sort (void)
{
int i, j, temp;
for ( i = 0; i < length - 1; ++i )
for ( j = i + 1; j < length; ++j )
if ( (ascending && array[i] > array[j]) || (!ascending && array[i] < array[j]) ) {
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int main (void)
{
int i;
void sort (void);
printf ("The array before the sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
ascending = 1;
sort ();
printf ("\n\nThe array after the ascending sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
ascending = 0;
sort ();
printf ("\n\nThe array after the descending sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
printf ("\n");
return 0;
}