// recursive my_quicksort routine: overloaded for word long my_quicksort(word *vals, long first, long last, long *indexes) { long splitpt; if (first < last) { splitpt = split(vals, first, last, indexes); my_quicksort(vals, first, splitpt - 1, indexes); my_quicksort(vals, splitpt +1, last, indexes); } return 0; }
long my_quicksort(double *vals, long first, long last, long *indexes) { long splitpt; if (first < last) { splitpt = split(vals, first, last, indexes); // get pivot my_quicksort(vals, first, splitpt - 1, indexes); my_quicksort(vals, splitpt +1, last, indexes); } return 0; }
// recursive my_quicksort routine: overloaded for 2d array of words long my_quicksort(word *vals[MAX_L], long first, long last, long len_each_val_in_words, long len_each_val_in_bytes, long *indexes) { TOTAL_NUM_QSORTS++; long splitpt; if (first < last) { splitpt = split(vals, first, last, len_each_val_in_words, len_each_val_in_bytes, indexes); my_quicksort(vals, first, splitpt - 1, len_each_val_in_words, len_each_val_in_bytes, indexes); my_quicksort(vals, splitpt +1, last, len_each_val_in_words, len_each_val_in_bytes, indexes); } return 0; }
void my_quicksort( int ** arrayptr, int leftIndex, int rightIndex) { /* partition the array , then recursively repartition it until its sorts*/ int pivotIndex; int * array = *arrayptr; if (leftIndex < rightIndex) { pivotIndex = leftIndex + ((rightIndex - leftIndex) / 2); pivotIndex = partition(arrayptr, leftIndex, rightIndex, pivotIndex); my_quicksort(arrayptr, leftIndex, pivotIndex - 1); my_quicksort(arrayptr, pivotIndex + 1, rightIndex); } }
// outer call for my_quicksort: overloaded function for word (unsigned long) long DoQuicksort(word *vals, long len, long *indexes) { long i; for(i = 0; i < len; i++) indexes[i] = i; // init: indexes are i.d. permutation my_quicksort(vals, 0, len-1, indexes); return 0; }
int main (int argc, char **argv) { int items = 100; SORTING_TYPE algorithm = heap; int counter = 0; int *array; if ( argc >=2 ) items = atoi(argv[1]); if ( argc >= 3 ) { if (strcmp (argv[2], "bubble") == 0) algorithm = bubble; else if (strcmp( argv[2], "heap") == 0) algorithm = heap; else if (strcmp (argv[2], "qsort") == 0) algorithm = quicksort; } srand(items); /* Build that array... */ array = calloc(items, sizeof *array); for (counter = 0; counter < items; counter++) { array[counter] = rand(); } printf ("Choosing sorting algorithm...\n"); /* TODO make this nicer */ if (algorithm == heap) { printf ("Chose 'heapsort'\n"); my_heapsort(&array, items); } else if (algorithm == bubble) { printf ("Chose 'bubblesort'\n"); my_bubblesort(&array, items); } else if (algorithm == quicksort) { printf ("Chose 'qsort'\n"); my_quicksort(&array, 0, items - 1); } else { printf ("No valid algorithm selected...\n"); return 255; } /* Now lets make sure it's actually sorted */ for (counter = 0; counter < items; counter++) { if (counter > 0) if (array[counter] > array[counter - 1]) { printf ("Item %d was out of order!\t%d > %d\n", counter, array[counter], array[counter -1]); } } printf ("%d items were sorted\n", counter); free(array); }
// outer call for my_quicksort: overloaded function for 2d array of words (unsigned long) long DoQuicksort(word *vals[MAX_L], long len, long len_each_val_in_words, long len_each_val_in_bytes, long *indexes) { long i; TOTAL_NUM_SPLITS=0; TOTAL_NUM_QSORTS=0; TOTAL_NUM_SWAPS=0; TOTAL_NUM_NO_SWAPS=0; for(i = 0; i < len; i++) indexes[i] = i; // init: indexes are i.d. permutation my_quicksort(vals, 0, len-1, len_each_val_in_words, len_each_val_in_bytes, indexes); return 0; }