/* --------------------------------------------------------- */ /* Test program for the sort utilities */ /* --------------------------------------------------------- */ #include <stdio.h> #include <stdlib.h> #include "sort.h" /* --------------------------------------------------------- */ /* User defined function used to compare two elements if you */ /* plan to sort elements according to increasing values */ /* --------------------------------------------------------- */ int increase_compare (const void *cle1, const void *cle2) { int *c1, *c2; c1 = (int*)cle1; c2 = (int*)cle2; if (*c1 > *c2) { return 1; } if (*c1 < *c2) { return -1; } return 0; } /* --------------------------------------------------------- */ /* User defined function used to compare two elements if you */ /* plan to sort elements according to decreasing values */ /* --------------------------------------------------------- */ int decrease_compare (const void *cle1, const void *cle2) { return -1 * increase_compare (cle1, cle2); } int main (int argc, char *argv[]) { int nb_values, i, cr; int *table; /* ------------------------------------------------------- */ /* Check command line arguments */ /* ------------------------------------------------------- */ if (argc != 2) { fprintf (stderr, "\nUsage: sort.test <number of elements>\n"); return 1; } nb_values = atoi(argv[1]); /* ------------------------------------------------------- */ /* Allocate memory to put random values */ /* ------------------------------------------------------- */ table = (int*)malloc(nb_values*sizeof(int)); if (table == NULL) { fprintf (stderr, "\nERROR: can not allocate memory !\n"); return 1; } /* ------------------------------------------------------- */ /* */ /* TESTING ISSORT */ /* */ /* ------------------------------------------------------- */ /* ------------------------------------------------------- */ /* Put random values into the array */ /* ------------------------------------------------------- */ for (i=0; i<nb_values; i++) { table[i] = rand(); } /* ------------------------------------------------------- */ /* Print the array */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nData to sort:"); fprintf (stdout, "\n-------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* Sort the array according to increasing values */ /* ------------------------------------------------------- */ cr = issort ( (void*)table, nb_values, sizeof(int), increase_compare ); if (cr == -1) { fprintf (stderr, "\nERROR: can not sort data !\n"); return 1; } /* ------------------------------------------------------- */ /* Print the sorted elements */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nSorted data using issort (increasing values):"); fprintf (stdout, "\n---------------------------------------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* Sort the array according to decreasing values */ /* ------------------------------------------------------- */ cr = issort ( (void*)table, nb_values, sizeof(int), decrease_compare ); if (cr == -1) { fprintf (stderr, "\nERROR: can not sort data !\n"); return 1; } /* ------------------------------------------------------- */ /* Print the sorted elements */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nSorted data using issort (decreasing values):"); fprintf (stdout, "\n---------------------------------------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* */ /* TESTING QKSORT */ /* */ /* ------------------------------------------------------- */ /* ------------------------------------------------------- */ /* Put random values into the array */ /* ------------------------------------------------------- */ for (i=0; i<nb_values; i++) { table[i] = rand(); } /* ------------------------------------------------------- */ /* Print the array */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nData to sort:"); fprintf (stdout, "\n-------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* Sort the array according to increasing values */ /* ------------------------------------------------------- */ cr = qksort ( (void*)table, nb_values, sizeof(int), 0, nb_values-1, increase_compare ); if (cr == -1) { fprintf (stderr, "\nERROR: can not sort data !\n"); return 1; } /* ------------------------------------------------------- */ /* Print the sorted elements */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nSorted data using qksort (increasing values):"); fprintf (stdout, "\n---------------------------------------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* Sort the array according to decreasing values */ /* ------------------------------------------------------- */ cr = qksort ( (void*)table, nb_values, sizeof(int), 0, nb_values-1, decrease_compare ); if (cr == -1) { fprintf (stderr, "\nERROR: can not sort data !\n"); return 1; } /* ------------------------------------------------------- */ /* Print the sorted elements */ /* ------------------------------------------------------- */ fprintf (stdout, "\n"); fprintf (stdout, "\nSorted data using qksort (decreasing values):"); fprintf (stdout, "\n---------------------------------------------"); fprintf (stdout, "\n\n"); for (i=0; i<nb_values; i++) { fprintf (stdout, "\n%d", table[i]); fflush (stdout); } fflush (stdout); /* ------------------------------------------------------- */ /* Free all memory and exit */ /* ------------------------------------------------------- */ free (table); return 0; }