CS157 Qsort
sort
command.
qsort
.
qsort
is like that.
#include <stdlib.h> void qsort(void *base, int count, int size, compare);
int numbers[] = {11,2,1957,10,14,1956,11,1,1955}; qsort(numbers, 9, sizeof(int), compare);
or
struct Who { char name[20]; double gpa; }; struct Who faculty[] = { {"Jack",3.9}, {"Bruce",3.5}, {"Darrell",0.2}, {"Dale",2.9} }; qsort(faculty, 4, sizeof(struct Who), compare_gpa);
int compare(const void *av, const void *bv) { const int *a = av, *b = bv; if (*a < *b) return -1; if (*a > *b) return 1; return 0; }
const void *
pointers.
const
means that we won’t change those values.
int compare(const void *av, const void *bv) { const int *a = av, *b = bv; if (*a < *b) return -1; if (*a > *b) return 1; return 0; }
(Really, any negative/positive/zero values are good enough.)
int compare(const void *av, const void *bv) { const int *a = av, *b = bv; if (*a < *b) return -1; return *a > *b; }
This does the same thing as the previous example.
int compare(const void *av, const void *bv) { const char *a = av, *b = bv; return strcmp(a,b); // perfect! }
The return values for a qsort
comparison function are exactly
like the return values of strcmp
.
struct Who { char name[20]; double gpa; }; int compare_gpa(const void *av, const void *bv) { const struct Who *a = av, *b = bv; if (a->gpa < b->gpa) return -1; if (a->gpa > b->gpa) return 1; return 0; }
This comparison function compares two structs, comparing them by GPA.
int compare(const void *av, const void *bv) { const int *a = av, *b = bv; if (*a < *b) return -1; if (*a > *b) return 1; return 0; } int main() { int numbers[] = {11,2,1957,10,14,1956,11,1,1955}; qsort(numbers, 9, sizeof(int), compare); for (int i=0; i<9; i++) printf("%d\n", numbers[i]); }
1 2 10 11 11 14 1955 1956 1957
struct Who { char name[20]; double gpa; }; int compare_gpa(const void *av, const void *bv) { const struct Who *a = av, *b = bv; if (a->gpa < b->gpa) return -1; if (a->gpa > b->gpa) return 1; return 0; } int compare_name(const void *av, const void *bv) { const struct Who *a = av, *b = bv; return -strcmp(a->name,b->name); } int compare_gpaname(const void *av, const void *bv) { const struct Who *a = av, *b = bv; if (a->gpa < b->gpa) return -1; if (a->gpa > b->gpa) return 1; return -strcmp(a->name,b->name); } int main() { struct Who faculty[] = { {"Jack",3.999}, {"Bruce",3.5}, {"Darrell",3.5}, {"Dale",2.9} }; qsort(faculty, 4, sizeof(struct Who), compare_gpaname); for (int i=0; i<4; i++) printf("%-8s %.2f\n", faculty[i].name, faculty[i].gpa); }
Dale 2.90 Darrell 3.50 Bruce 3.50 Jack 4.00