/* program to time several sorting algorithms on data sets of various sizes */ #include #include // for malloc, free #include // for time /********************************************************************** * Name: * * Assignment nameComparison of Sorts * * Assignment for * ***********************************************************************/ * ********************************************************************* * Academic honesty certification: * * Written/online sources used: * * Program sort-comparisons.c by henry m Walker from CS 415 Lab * * [include textbook(s), CS 415 labs or readings; * * complete citations for Web or other written sources] * * Help obtained * * [indicate names of instructor, class mentors * * or evening tutors, consulted according to class policy; * * write "none" if none of these sources used] * * My signature below confirms that the above list of sources * * is complete AND that I have not talked to anyone else * * (e.g., CSC 415 students) about the solution to this problem * * * * Signature: * ***********************************************************************/ /* straight selection sort */ void straightSelection (int a [ ], int n) { int i, j, smallIndex; int temp; // put largest remaining element in a[i] for (i = n-1; i >= 0; i--) { // find largest in a[i..n-1] smallIndex = i; for (j = i-1; j >= 0; j--) { if (a[smallIndex] < a[j]) smallIndex = j; } // swap smallest to a[i] temp = a[smallIndex]; a[smallIndex] = a[i]; a[i] = temp; } } /* driver program for testing and timing sorting algorithms */ int main ( ) { // print headings printf (" Data Set Times\n"); printf ("Algorithm Size Ascending Order Random Order Descending Order\n"); int size; for (size = 10000; size <= 160000; size *= 2) { // create control data set arrays int * asc = (int *) malloc (size * sizeof(int)); //array with ascending data int * ran = (int *) malloc (size * sizeof(int)); //array with random data int * des = (int *) malloc (size * sizeof(int)); // array with descending data int i; for (i = 0; i< size; i++) { asc[i] = 2*i; ran[i] = rand(); des[i] = 2*(size - i - 1); } // timing variables clock_t start_time, end_time; double elapsed_time; // copy to test arrays int * tempAsc = malloc (size * sizeof(int)); int * tempRan = malloc (size * sizeof(int)); int * tempDes = malloc (size * sizeof(int)); for (i = 0; i< size; i++) { tempAsc[i] = asc[i]; tempRan[i] = ran[i]; tempDes[i] = des[i]; } // timing for straight selection sort printf ("selection sort %7d", size); // ascending data start_time = clock (); straightSelection (tempAsc, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%14.1lf", elapsed_time); // tandom data start_time = clock (); straightSelection (tempRan, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%15.1lf", elapsed_time); // descending data start_time = clock (); straightSelection (tempDes, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%15.1lf", elapsed_time); printf ("\n"); /* print results of sorting (ascending/randonm/descending data) for (i = 0; i < 15; i++) { printf ("%10d %10d %10d\n", tempAsc[i], tempRan[i], tempDes[i]); } */ // clean up copies of test arrays free (tempAsc); free (tempRan); free (tempDes); // clean up original test arrays free (asc); free (ran); free (des); } // end of loop for testing procedures with different array sizes return 0; }