/* program to time several versions of the merge sort algorithm on data sets of various sizes */ #include #include // for malloc, free #include // for time /** ******************************************************************************* * structure to identify both the name of a sorting algorithm and * * a pointer to the function that performs the sort * * the main function utilizes this struct to define an array of * * the sorting algorithms to be timed by this program. * *********************************************************************************/ typedef struct sorts { char * name; /**< the name of a sorting algorithm as text */ void (*sortProc) (int [ ], int); /**< the procedure name of a sorting function */ } sorts; /***************************************************************************************** * * * * * * * * * traditional recursive merge sort and merge procedures * * * * * * * * * ****************************************************************************************/ /** ************************************************************************************** * tradtional merge Sort procedure, renamed from * * source: tutorialpoint.com: * * https://www.tutorialspoint.com/cplusplus-program-to-implement-merge-sort * * See URL for parameters, etc. * *****************************************************************************************/ void tutorialPointMerge (int *array, int l, int m, int r) { int i, j, k, nl, nr; //size of left and right sub-arrays nl = m-l+1; nr = r-m; int larr[nl], rarr[nr]; //fill left and right sub-arrays for(i = 0; i= aInit.length, in which case, only the * * valid part of aInit[start1] is copied * ****************************************************************************************/ void iterMergeDynArrays (int array [ ], int aInitLength, int start1, int start2, int end2) { if (start2 > aInitLength) start2 = aInitLength; int end1 = start2-1; if (end2 > aInitLength) end2 = aInitLength; int pos = start1; //size of left and right sub-arrays int lSize = start2 - start1; int rSize = end2 - start2; // copy array segments to local arrays int * lArr = (int *) malloc (lSize * sizeof (int)); int * rArr = (int *) malloc (rSize * sizeof (int)); //fill left and right sub-arrays int i, j; for(i = 0; i a[i+1]) return "NO"; } return "ok"; } /** ************************************************************************************** * driver program for testing and timing sorting algorithms * *****************************************************************************************/ int main ( ) { #define numAlgs 4 sorts sortProcs [numAlgs] = {{"trad. recursive merge sort", tradRecMergeSort}, {"rec. merge sort w/2 arrays", recMerge2Arrays}, {"iter. merge w/trad. merge ", iterAlgWkthTradMerge}, {"2-array interative sort ", iter2ArrMergeSort}}; //size variables int maxDataSetSize = 40960000; int algTimeLimit = 15; // do not print results after algorithm taks this long (in seconds) //arrays to determine which algorithms are to be run // 1 = run algorithm; 0 = do not run (alg has taken too long in past int algAscActive [numAlgs]; int algRanActive [numAlgs]; int algDesActive [numAlgs]; // set maxRecSize to largest size avaiable before runtime stack overflow for recursive algorithms int maxRecSize = 1600000; maxRecSize = maxDataSetSize; // this line allows all algorithms to be called, regardless of their details // see assignment, problem 4b for details // initially all algorithms active for ascending, random, and descendind data int numSort; ; for (numSort = 0; numSort < numAlgs; numSort++) { algAscActive[numSort] = 1; algRanActive[numSort] = 1; algDesActive[numSort] = 1; } // randomize random number generator's seed srand (time ((time_t *) 0) ); srandom (time ((time_t *) 0) ); // print headings printf (" Data Set Times\n"); printf ("Algorithm Size Ascending Order Random Order Descending Order\n"); int size; for (size = 100000; size <= maxDataSetSize; size *= 2) { // create and initialize 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; /* loop to test successive sorting procedures */ // copy to test arrays int * tempAsc = malloc (size * sizeof(int)); int * tempRan = malloc (size * sizeof(int)); int * tempDes = malloc (size * sizeof(int)); // break output for this array sze printf ("\n"); /* iterate through sorting algorithms */ for (numSort = 0; numSort < numAlgs; numSort++) { for (i = 0; i< size; i++) { tempAsc[i] = asc[i]; tempRan[i] = ran[i]; tempDes[i] = des[i]; } // timing for sorting algorithm printf ("%14s %8d", sortProcs[numSort].name, size); // run-time stack exceeded for qicksort for large ordered arrays if (!algAscActive[numSort]) { printf (" --- --"); } else { // ascending data start_time = clock (); sortProcs[numSort].sortProc (tempAsc, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%14.1lf", elapsed_time); printf (" %2s", checkAscValues (tempAsc, size)); algAscActive[numSort] = (elapsed_time <= algTimeLimit) && ((numSort!= 0) || (size < maxRecSize)); } if (!algRanActive[numSort]) { printf (" --- --"); } else { // random data start_time = clock (); sortProcs[numSort].sortProc (tempRan, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%15.1lf", elapsed_time); printf (" %2s", checkAscending (tempRan, size)); algRanActive[numSort] = (elapsed_time <= algTimeLimit) && ((numSort!= 0) || (size < maxRecSize)); } // run-time stack exceeded for qicksort for large ordered arrays if (!algDesActive[numSort]) { printf (" --- --"); } else { // descending data start_time = clock (); sortProcs[numSort].sortProc (tempDes, size); end_time = clock(); elapsed_time = (end_time - start_time) / (double) CLOCKS_PER_SEC; printf ("%15.1lf", elapsed_time); printf (" %2s", checkAscValues (tempDes, size)); algDesActive[numSort] = (elapsed_time <= algTimeLimit) && ((numSort!= 0) || (size < maxRecSize)); } printf ("\n"); } // 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; }