Automated Grading Log for CS113 -- HW8 userid: samarga Name: Amarga, Sheila Log created: Fri Dec 13 09:40:25 EST 1996 SUBMISSIONS =========== samarga Submitted hw8-sorts.c at Wed Dec 4 15:39:37 1996 COMPILATION =========== gccx -g -I/home/course/cs113/F96/include -Wall -c hw8-sorts.c gccx -g -I/home/course/cs113/F96/include -Wall -o hw8-acc hw8-sorts.o ../source/hw8-acc.o gccx -g -I/home/course/cs113/F96/include -Wall -o hw8-eff hw8-sorts.o ../source/hw8-eff.o EXECUTION TESTS ========= ===== ============================================================ hw8-acc /home/course/cs113/GradesF96/HW8/inputs/short* Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/short.dec . . . done. Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/short.inc . . . done. Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/short.random . . . done. ACCURACY TESTING Insertion Sort: /home/course/cs113/GradesF96/HW8/inputs/short.dec: 9 10 11 12 13 14 15 15 16 16 17 18 /home/course/cs113/GradesF96/HW8/inputs/short.inc: 0 0 2 2 4 4 6 6 8 9 10 11 /home/course/cs113/GradesF96/HW8/inputs/short.random: 2749 4086 5627 5758 7419 10113 12767 16212 16838 17515 23010 31051 Quick Sort: /home/course/cs113/GradesF96/HW8/inputs/short.dec: 9 10 11 12 13 14 15 15 16 16 17 18 /home/course/cs113/GradesF96/HW8/inputs/short.inc: 0 0 2 2 4 4 6 6 8 9 10 11 /home/course/cs113/GradesF96/HW8/inputs/short.random: 2749 4086 5627 5758 7419 10113 12767 16212 16838 17515 23010 31051 Merge Sort: /home/course/cs113/GradesF96/HW8/inputs/short.dec: 9 10 11 12 13 14 15 15 16 16 17 18 /home/course/cs113/GradesF96/HW8/inputs/short.inc: 0 0 2 2 4 4 6 6 8 9 10 11 /home/course/cs113/GradesF96/HW8/inputs/short.random: 2749 4086 5627 5758 7419 10113 12767 16212 16838 17515 23010 31051 Shell Sort: /home/course/cs113/GradesF96/HW8/inputs/short.dec: 9 10 11 12 13 14 15 15 16 16 17 18 /home/course/cs113/GradesF96/HW8/inputs/short.inc: 0 0 2 2 4 4 6 6 8 9 10 11 /home/course/cs113/GradesF96/HW8/inputs/short.random: 2749 4086 5627 5758 7419 10113 12767 16212 16838 17515 23010 31051 Done: hw8-acc /home/course/cs113/GradesF96/HW8/inputs/short.dec /home/course/cs113/GradesF96/HW8/inputs/short.inc /home/course/cs113/GradesF96/HW8/inputs/short.random ============================================================ hw8-eff /home/course/cs113/GradesF96/HW8/inputs/long* Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/long.dec . . . done. Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/long.inc . . . done. Loading data from file: /home/course/cs113/GradesF96/HW8/inputs/long.random . . . done. EFFICIENCY TESTING Insertion Sort ==================== Sort: Insertion Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.dec Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.02000 5.000e-07 1.887e-05 S 400 0.05000 3.125e-07 2.086e-05 S 800 0.18000 2.813e-07 3.366e-05 S 1600 0.74000 2.891e-07 6.269e-05 S 3200 3.12000 3.047e-07 1.208e-04 Sort: Insertion Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.inc Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.00000 1.250e-12 8.345e-11 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.00000 7.813e-14 1.694e-11 S 3200 0.00000 1.953e-14 7.744e-12 S 6400 0.00000 4.883e-15 3.566e-12 S 12800 0.01000 6.104e-11 8.261e-08 S 25600 0.02000 3.052e-11 7.697e-08 Sort: Insertion Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.random Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.01000 1.000e-06 2.171e-05 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.02000 1.250e-07 8.345e-06 S 800 0.09000 1.406e-07 1.683e-05 S 1600 0.40000 1.563e-07 3.389e-05 S 3200 1.55000 1.514e-07 6.001e-05 Quick Sort ==================== Sort: Quick Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.dec Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.01000 6.250e-08 4.173e-06 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.01000 9.766e-10 3.872e-07 S 6400 0.04000 9.766e-10 7.131e-07 S 12800 0.08000 4.883e-10 6.609e-07 S 25600 0.18000 2.747e-10 6.927e-07 Sort: Quick Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.inc Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.01000 1.000e-06 2.171e-05 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.00000 1.250e-12 8.345e-11 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.00000 7.813e-14 1.694e-11 S 3200 0.01000 9.766e-10 3.872e-07 S 6400 0.04000 9.766e-10 7.131e-07 S 12800 0.08000 4.883e-10 6.609e-07 S 25600 0.19000 2.899e-10 7.312e-07 Sort: Quick Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.random Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.01000 6.250e-08 4.173e-06 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.04000 3.906e-09 1.549e-06 S 6400 0.07000 1.709e-09 1.248e-06 S 12800 0.14000 8.545e-10 1.157e-06 S 25600 0.34000 5.188e-10 1.308e-06 Merge Sort ==================== Sort: Merge Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.dec Sorted? Size Time (sec) n^2/Time n log n/Time U 100 0.00000 2.000e-11 4.343e-10 U 200 0.01000 2.500e-07 9.437e-06 U 400 0.01000 6.250e-08 4.173e-06 U 800 0.01000 1.562e-08 1.870e-06 U 1600 0.03000 1.172e-08 2.541e-06 U 3200 0.08000 7.812e-09 3.098e-06 U 6400 0.16000 3.906e-09 2.853e-06 U 12800 0.30000 1.831e-09 2.478e-06 U 25600 0.70000 1.068e-09 2.694e-06 Sort: Merge Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.inc Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.01000 6.250e-08 4.173e-06 S 800 0.02000 3.125e-08 3.740e-06 S 1600 0.03000 1.172e-08 2.541e-06 S 3200 0.09000 8.789e-09 3.485e-06 S 6400 0.19000 4.639e-09 3.387e-06 S 12800 0.28000 1.709e-09 2.313e-06 S 25600 0.71000 1.083e-09 2.732e-06 Sort: Merge Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.random Sorted? Size Time (sec) n^2/Time n log n/Time U 100 0.00000 2.000e-11 4.343e-10 U 200 0.01000 2.500e-07 9.437e-06 U 400 0.01000 6.250e-08 4.173e-06 U 800 0.02000 3.125e-08 3.740e-06 U 1600 0.03000 1.172e-08 2.541e-06 U 3200 0.08000 7.812e-09 3.098e-06 U 6400 0.16000 3.906e-09 2.853e-06 U 12800 0.34000 2.075e-09 2.809e-06 U 25600 0.71000 1.083e-09 2.732e-06 Shell Sort ==================== Sort: Shell Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.dec Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.01000 6.250e-08 4.173e-06 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.04000 3.906e-09 1.549e-06 S 6400 0.08000 1.953e-09 1.426e-06 S 12800 0.15000 9.155e-10 1.239e-06 S 25600 0.32000 4.883e-10 1.231e-06 Sort: Shell Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.inc Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.01000 6.250e-08 4.173e-06 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.01000 9.766e-10 3.872e-07 S 6400 0.05000 1.221e-09 8.914e-07 S 12800 0.11000 6.714e-10 9.087e-07 S 25600 0.21000 3.204e-10 8.082e-07 Sort: Shell Sort File: /home/course/cs113/GradesF96/HW8/inputs/long.random Sorted? Size Time (sec) n^2/Time n log n/Time S 100 0.00000 2.000e-11 4.343e-10 S 200 0.00000 5.000e-12 1.887e-10 S 400 0.00000 1.250e-12 8.345e-11 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.02000 7.812e-09 1.694e-06 S 3200 0.04000 3.906e-09 1.549e-06 S 6400 0.09000 2.197e-09 1.605e-06 S 12800 0.22000 1.343e-09 1.817e-06 S 25600 0.45000 6.866e-10 1.732e-06 Done: hw8-eff /home/course/cs113/GradesF96/HW8/inputs/long.dec /home/course/cs113/GradesF96/HW8/inputs/long.inc /home/course/cs113/GradesF96/HW8/inputs/long.random SOURCE FILES ====== ===== /* File: hw8-sorts.c * ------------------ * Author: Sheila Amarga * Class: CS 113 * * Assignment: HW8, Sorting Algorithms * * Due Date: 4 December 1996 * Last modified: 4 November 1996 * ------------------------------------------------------------------- * * Overview * ======== * Four sorts have been written, Insertion Sort, Quick Sort, Shell Sort, and * Merge Sort, along with with the code to implement the functions * NumOfIntSorts, IntSortName, and IntSort. These functions and sorts are used * to run the output file of "hw8-test" which is to use * " ~cs113/F96/include/data* " as the data file to be read in. * * Known Bugs * ========== * None * */ #include #include #include "strlib.h" #include "hw8-sorts.h" /* Function Protypes */ void InsertionSort (int array[], int length); void QuickSort(int array[], int first, int last); void Partition(int array[], int *left, int *right); void Merge(int a[], int lengthOfA, int b[], int lengthOfB, int c[]); void MergeSort (int array[], int length); void ShellSort (int array[], int length); void DeltaInsertionSort(int array[], int i, int delta, int length); /* Function: InsertionSort * Usage: InsertionSort(array, length); * ------------------------------------ * This function sorts the array in-place meaning that no extra memory is * needed. It sorts by removing a value from the array, which leaves a "hole" * in the array. Then the above valuess are shifted down until we find the * correct place to insert the value. This process repeats in for the rest or * the other values untill finally, we complete the sort by inserting the last * value in the correct place. * Assuming there are "n" elements in the array, we must index through "n - 1" * entries. For each entry, we may need to examine and shift up to "n - 1" * other entries. For this reason, sorting is a time-consuming process. * * Insertion Sort runs in time O(n^2) */ void InsertionSort(int array [], int length){ int i, j, value; bool seeking; /* For each i in the range 1:n-1, let "value" be array[i]. Then * insert "value" into the subarray A[0:i -1] in ascending order */ for (i = 1; i < length; ++i) { /* Move each number bigger than "value" in array[0:i -1] one space to the * right */ value = array[i]; j = i; seeking = (array[j -1] > value); while (seeking) { array[j] = array[j - 1]; /* Move array[j -1] one space to the right */ j--; if (j > 0) { seeking = (array[j -1] > value); } else { seeking = FALSE; } } /* Move "value" into hole opened up by moving previous values to the * right */ array[j] = value; } } /* Function: QuickSort * Usage: QuickSort(array, begining, end); * ------------------------------------- * This function is the code for the sort Quick Sort. The quicksort algorithm * works by partitioning the array to be sorted, then recursively sorting each * partition. In the function Partition, one of the array elements is selected * as a pivot value. Values smaller than the pivot value are placed to the left * of the pivot, while larger values are placed to the right. * * Quicksort executes in O(n log n) on average, and O(n^2) in the worst-case. */ void QuickSort(int array[], int first, int last){ /*to sort the subarray array[begining:end] of "array" into ascending order */ int left, right; if (first < last) { /* Initially left and rigth point to the first and last values */ left = first; right = last; /* partitions array[first:last] into A[first:right] and * array[left:last] */ Partition(array,&left,&right); QuickSort(array,first,right); QuickSort(array,left,last); } } /* Function: Partition * Usage: Partition(array,&left,&right); * ------------------------------------- * This function chooses the middle value of the array as the pivot point. * Indices are run starting at both ends of the array. Index "left" starts * on the left and selects an element that is larger than the pivot, while * index "right" starts on the right and selects an element that is smaller * than the pivot. These elements are then exchanged. */ void Partition(int array[], int *left, int *right){ int pivot, temp; /* choose the middle key as the pivot */ pivot = array[(*left + *right) / 2] ; do{ /* Find leftmost "left" such that array[left] >= pivot.*/ while (array[*left] < pivot){ (*left)++; } /* Find rightmost "right" such that array[right] <= pivot.*/ while (array[*right] > pivot){ (*right)--; } /* If "left" and "right" didn't cross over one another, swap */ if (*left <= *right){ temp = array[*left]; array[*left] = array[*right]; array[*right] = temp; /* Move "left" one space right */ (*left)++; /* Move "right" one space left */ (*right)--; } /* while the "left" and "right" pointers haven't crossed yet */ } while (*left <= *right); } /* Function: Merge * Usage: Merge(sourceArray1, length1, sourceArray2, length2, * destinationArray); * ---------------------------------------------------------- * This function merges two already-sorted arrays into a single sorted array. */ void Merge(int a[], int lengthOfA, int b[], int lengthOfB, int c[]){ int i = 0, /* Index into a. */ j = 0, /* Index into b. */ k = 0; /* Index into c. */ while (i < lengthOfA && j < lengthOfB){ if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } /* Pick up any remaining elements in "a". */ while (i < lengthOfA){ c[k++] = a[i++]; } /* Pick up any remaining elements in "b". */ while (j < lengthOfB){ c[k++] = b[j++]; } } /* Function: MergeSort * Usage: MergeSort(array, length); * -------------------------------- * This function sorts an array using a divide and conquer strategy. * * Runs in time O(n log n). */ void MergeSort(int array[], int length) { int position; /* Looping variable */ int halfLength; /* Half the length of the array. */ int *workSpace; /* Temporary space to work in. */ if (length == 1){ return; } /* Point at which to split array. */ halfLength = length / 2; MergeSort(array, length - halfLength); MergeSort(array + halfLength, halfLength); /* Allocates memory for workSpace */ workSpace = (int *) calloc(length, sizeof(int)); Merge(array, halfLength, array + halfLength, length - halfLength, workSpace); for (position = 0; position < length; position++){ array[position] = workSpace[position]; } free(workSpace); } /* Function: Shell Sort * Usage: ShellSort(array, length); * -------------------------------- * This function is the sort called Shell Sort. Shell sort improves on the * efficiency of insertion sort by quickly shifting values to their * destination. * We first partition our array to be sorted into (say) thirds. We take the * first item in each third and insertion sort them (quick since there are only * three of them). We then take the second item of each third and insertion * sort them. We repeat this for each triplet. * Having done this we then repeat the process, but this time partitioning into * ninths. We are now insertion sorting groups of nine each time, but since * they are roughly sorted, this will still be quick. * We then repeat with groups of 27, and so on, until we eventually have "n" * groups with just 1 element in, at which point we are doing an insertion sort * * Average sort time is O(n^1.25), while worst-case time is O(n^1.5). */ void ShellSort(int array[], int length){ int i, delta; /* delta is used for the spacing to be used in the DeltaInsertionSort */ delta = length; do{ delta = 1 + delta / 3; for (i = 0; i < delta; ++i){ DeltaInsertionSort(array, i, delta, length); } } while (delta > 1); } /* Function: DeltaInsertionSort * Usage: DeltaInsertion(array, i, delta, length); * ----------------------------------------------- * This function is basically an InsertionSort but instead of incrementing by 1 * it increments by "delta" */ void DeltaInsertionSort(int array[], int i, int delta, int length) { int j, k, numToInsert; bool notDone; j = i + delta; while (j < length){ /* obtain a new numToInsert */ numToInsert = array[j]; /* move each value > numToInsert rightward by delta spaces to open up a * hole in which to place the numToInsert */ k = j; notDone = TRUE; do{ if (array[k - delta] <= numToInsert){ notDone = FALSE; } else{ array[k] = array[k - delta]; k -= delta; if (k == i){ notDone = FALSE; } } } while (notDone); /* put numToInsert in hole array[k] opened by moving values > numToInsert * to the right */ array[k] = numToInsert; /* consider next numToInsert at an increment of delta to the right */ j += delta; } } int NumOfIntSorts(void){ int numSorts; /* Set numOfSorts equal to the number of sorts in the file hw8-sorts.c */ numSorts = 4; return numSorts; } string IntSortName(int sortNum){ if ((0 <= sortNum) && (sortNum < (NumOfIntSorts()) )){ switch (sortNum){ case 0: return ("Insertion Sort"); case 1: return ("Quick Sort"); case 2: return ("Merge Sort"); case 3: return ("Shell Sort"); default: return (0); } } else{ return ("No sort performed.\n"); } } void IntSort(int sort, int array[], int size){ if (0 <= sort && sort < (NumOfIntSorts()) ){ switch (sort){ case 0: (InsertionSort(array, size)); break; case 1: (QuickSort(array, 0, size - 1)); break; case 2: (MergeSort (array, size)); break; case 3: (ShellSort(array, size)); break; } } } -- END OF LOG FILE FOR samarga --