-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsortFunc.c
74 lines (52 loc) · 1.35 KB
/
qsortFunc.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int compare_scores(const void* score_a, const void* score_b){
int a =*(int*)score_a;
int b =*(int*)score_b;
return a-b;
}
int compare_scores_desc(const void* score_a, const void* score_b){
int a = *(int*)score_a;
int b =*(int*)score_b;
return b-a;
}
typedef struct{
int width;
int height;
}rectangle;
int compare_areas(const void* a, const void* b){
rectangle* area1 = (rectangle*)a;
rectangle* area2 = (rectangle*)b;
int area_a = (area1->width * area1->height);
int area_b = (area2->width * area2->height);
return area_a - area_b;
//return sizeof(area1)-sizeof(area2);
}
int compare_names(const void* a,const void* b){
char** name1 = (char**)a;
char** name2 = (char**)b;
return strcmp(*name1,*name2);
}
int compare_areas_desc(const void* a, const void* b){
return compare_areas(b,a);
}
int compare_names_desc(const void* a,const void* b){
return compare_names(b,a);
}
int main(){
int scores[]={543,323,32,554,11,3,112};
int i;
qsort(scores,7,sizeof(int),compare_scores_desc);
puts("These are the scores in order:");
for(i=0;i<7;i++){
printf("Score = %i\n",scores[i]);
}
char* names[]={"Karen","Mark","Brett","Molly"};
qsort(names,4,sizeof(char*),compare_names);
puts("These are the names in order:");
for(i = 0; i<4;i++){
printf("%s\n",names[i]);
}
return 0;
}