-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqSortCompare.c
72 lines (48 loc) · 1.98 KB
/
qSortCompare.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
61
62
63
64
65
66
67
68
69
70
71
72
int FirstColumnCompare(const void *pa, const void *pb){
// pointer to const. int // This is a derefernce (indirection) operator.
const double *a = *(const double **) pa;
const double *b = *(const double **) pb;
if(a[0] == b[0]){
if(a[1] - b[1] < 0) return -1;
else return 1;
}
else{
if(a[0] - b[0] < 0) return -1;
else return 1;
}
}
int SecondColumnCompare(const void *pa, const void *pb){
// pointer to const. int // This is a derefernce (indirection) operator.
const double *a = *(const double **) pa;
const double *b = *(const double **) pb;
if(a[1] == b[1]){
if(a[2] - b[2] < 0) return -1;
else return 1;
}
else{
if(a[1] - b[1] < 0) return -1;
else return 1;
}
}
int TwoColumnCompareTest(){
double** testArray;
int length = 40;
testArray = malloc(length*sizeof(double*)); // rows
for(j=0; j<length; j++){
testArray[j] = malloc(3*sizeof(double));
testArray[j][0] = (double) 1./(rand() % 7 + 1.);
testArray[j][1] = (double) 1./(rand() % 7 + 1.);
testArray[j][2] = (double) 1./(rand() % 7 + 1.);
printf("\n%.6f \t \t %.6f \t \t %.6f", testArray[j][0], testArray[j][1], testArray[j][2]);
}
printf("\n\n");
qsort(testArray, length, sizeof(testArray[0]), FirstColumnCompare);
printf("\n\n");
for(j=0; j<length; j++) printf("\n%.6f \t \t %.6f \t \t %.6f", testArray[j][0], testArray[j][1], testArray[j][2]);
qsort(testArray, length, sizeof(testArray[0]), SecondColumnCompare);
printf("\n\n");
for(j=0; j<length; j++) printf("\n%.6f \t \t %.6f \t \t %.6f", testArray[j][0], testArray[j][1], testArray[j][2]);
for(j=0; j<length; j++) free(testArray[j]);
free(testArray);
return 0;
}