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
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
printData(d,n)
int *d,n;
{
int i;
for (i=0; i<n; i++)
printf("data[%d] = %d\n", i, d[i]);
}
selectionSortAscending(d, n)
int *d, n;
{
int i, j, tmp;
for (i = 0; i < n; i++) {
tmp = d[i];
for (j = i + 1; j < n; j++) {
if(tmp > d[j]){
d[i] = d[j];
d[j] = tmp;
tmp = d[i];
}
}
}
}
insertionSortDescending(d, n)
int *d, n;
{
int i,j,tmp;
for (i = 1; i < n; i++){
for (j = i; d[j] > d[j-1] && j != 0; j--){
tmp = d[j];
d[j] = d[j-1];
d[j-1] = tmp;
}
}
}
main(argc, argv, envp)
int argc;
char **argv, **envp;
{
char buf[1024];
int n, *data, i;
if (argc !=2 ) {
printf("usage: sortAssignment1 #elements\n");
exit(1);
}
n = atoi(argv[1]);
data = calloc(n, sizeof(int));
for (i=0; i<n; i++) {
gets(buf);
data[i] = atoi(buf);
}
printf("unsorted data:\n"); sleep(1);
printData(data, n);
selectionSortAscending(data, n);
printf("After selection sort\n"); sleep(1);
printData(data, n);
insertionSortDescending(data, n);
printf("After insertion sort\n"); sleep(1);
printData(data, n);
}
|