// Driver for generalized quicksort with comparison function #include #include #include // Type is a C string - must be defined before #including quicksort.c typedef const char * VALTYPE; #include "quicksort.c" // Function to display a list of values void display(VALTYPE v[], int nitems) { for (int i = 0; i < nitems; ++i) { printf( "%s ", v[i]); } printf("\n"); } // Compare ignoring case bool compare1(VALTYPE v1, VALTYPE v2) { return (strcasecmp(v1, v2) < 0) ? true : false; } // Reverse alphabetic, ignoring case bool compare2(VALTYPE v1, VALTYPE v2) { return (strcasecmp(v1, v2) < 0) ? false : true; } // Case sensitive compare bool compare3(VALTYPE v1, VALTYPE v2) { return (strcmp(v1, v2) < 0) ? true : false; } // Testing interface - give values to sort on command line int main(int argc, char *argv[]) { if (argc < 2) { fprintf(stderr,"Usage: %s v1 v2 v3 ...\n", argv[0]); return -1; } const int nitems = argc - 1; VALTYPE *v = (VALTYPE *)malloc(nitems * sizeof(VALTYPE)); for (int i = 1; i <= nitems; ++i) { v[i-1] = argv[i] ; } display(v, nitems); printf("sorted by compare1: "); quicksort(v, nitems, compare1); display(v, nitems); printf("sorted by compare2: "); quicksort(v, nitems, compare2); display(v, nitems); printf("sorted by compare3: "); quicksort(v, nitems, compare3); display(v, nitems); return 0; }