-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex.8.13.c
42 lines (33 loc) · 898 Bytes
/
ex.8.13.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
// Program to sort an array of integers into ascending order
#include <stdio.h>
void sort (int a[], int n, int ascending)
{
int i, j, temp;
for ( i = 0; i < n - 1; ++i )
for ( j = i + 1; j < n; ++j )
if ( (ascending && a[i] > a[j]) || (!ascending && a[i] < a[j]) ) {
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 };
void sort (int a[], int n, int ascending);
printf ("The array before the sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
sort (array, 16, 1);
printf ("\n\nThe array after the ascending sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
sort (array, 16, 0);
printf ("\n\nThe array after the descending sort:\n");
for ( i = 0; i < 16; ++i )
printf ("%i ", array[i]);
printf ("\n");
return 0;
}