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