Automated Grading Log for CS113 -- HW8 userid: samek Name: Kadziela, Samuel Log created: Fri Dec 13 09:40:57 EST 1996 SUBMISSIONS =========== samek Submitted hw8-sorts.c at Wed Dec 4 23:13:04 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 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 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 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 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 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 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.04000 1.562e-08 3.389e-06 S 3200 0.07000 6.836e-09 2.710e-06 S 6400 0.15000 3.662e-09 2.674e-06 S 12800 0.33000 2.014e-09 2.726e-06 S 25600 0.63000 9.613e-10 2.424e-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.08000 7.812e-09 3.098e-06 S 6400 0.16000 3.906e-09 2.853e-06 S 12800 0.32000 1.953e-09 2.643e-06 S 25600 0.64000 9.766e-10 2.463e-06 Sort: Merge 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.01000 2.500e-07 9.437e-06 S 400 0.00000 1.250e-12 8.345e-11 S 800 0.01000 1.562e-08 1.870e-06 S 1600 0.05000 1.953e-08 4.236e-06 S 3200 0.09000 8.789e-09 3.485e-06 S 6400 0.16000 3.906e-09 2.853e-06 S 12800 0.32000 1.953e-09 2.643e-06 S 25600 0.70000 1.068e-09 2.694e-06 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.00000 1.250e-12 8.345e-11 S 800 0.00000 3.125e-13 3.740e-11 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.02000 1.953e-09 7.744e-07 S 6400 0.04000 9.766e-10 7.131e-07 S 12800 0.10000 6.104e-10 8.261e-07 S 25600 0.20000 3.052e-10 7.697e-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.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.01000 1.562e-08 1.870e-06 S 1600 0.01000 3.906e-09 8.471e-07 S 3200 0.02000 1.953e-09 7.744e-07 S 6400 0.04000 9.766e-10 7.131e-07 S 12800 0.08000 4.883e-10 6.609e-07 S 25600 0.16000 2.441e-10 6.157e-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.00000 1.250e-12 8.345e-11 S 800 0.02000 3.125e-08 3.740e-06 S 1600 0.02000 7.812e-09 1.694e-06 S 3200 0.03000 2.930e-09 1.162e-06 S 6400 0.07000 1.709e-09 1.248e-06 S 12800 0.14000 8.545e-10 1.157e-06 S 25600 0.33000 5.035e-10 1.270e-06 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.01000 2.500e-07 9.437e-06 S 400 0.04000 2.500e-07 1.669e-05 S 800 0.12000 1.875e-07 2.244e-05 S 1600 0.56000 2.188e-07 4.744e-05 S 3200 2.39000 2.334e-07 9.254e-05 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.05000 7.813e-08 9.350e-06 S 1600 0.30000 1.172e-07 2.541e-05 S 3200 1.23000 1.201e-07 4.762e-05 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-------------------- */ /*************************************************************************** * * * Samuel Kadziela * CS113 [Fall 96] * Due by December 4, 1996 * Assignment #8 * * * Purpose: * -------- * The purpose of this file is to provide a basic sorting tool for * integer arrays. It contains three different sort programs, each of * which runs at different times and is better with different organization of * data. For example, some sorts which run very fast on random numbers are * not very good with numbers sorted in decreasing or increasing order. I * included Merge Sort, Quick Sort and Insertion Sort. * * * Methods: * -------- * This file contains three functions which are exported to the calling * programs, and several sorting functions, which are static and * unavailable to the user. These functions are only used within the * particular sorts to perform sorting. The functions which are exported * are: NumOfIntSorts, which returns the number of sorts available in this * program - 3; * IntSortName, which returns the names of the sorts implemented here, * and * IntSort, which takes in the number of sort that the user wants to use * and uses it to sort given array. * * Each individual sort is described below, with possible enhancements on * the bottom of this boilerplate. * * Insertion Sort: This is an iterative sort which moves record by record * each time inserting the new record in the appropriate place in the * already ordered subarray. This way, every time a smaller value is * encountered, it is swithed with the current smallest value, and * this is done for all values of the array. * * Quick Sort: The underlying idea is to break, or partition, the array * and sort the subarrays, except that the partitioning is done until * each subarray holds one value in it, and thus is already sorted. * The subarrays are partitioned so that everything in that subarray * is smaller than tha pivot, and everything in the other subarray is * greater than the pivot. This is a recursive function, which calls * the function Partition to do the breaking up of the array, and it * calls on itself to than "sort" the two subarrays. * * Merge Sort: This is similat in technique to Quick Sort, except Merge * Sort partitiones the array in half, and then sorts the subarrays, * while Quick Sort partitiones the array according to some pivot * element which is picked in the beginning of the sort of specified * by the user. Merge Sort also continues to partition the array * until each subarray holds only one value in it, and then it Merges * the already sorted subarrays together. * * Variables used and Data-structures: * ----------------------------------- * * There are no special data structures used. * Below follows a list and a description of all variables used in this * file and inside all the functions: * * * NUMBER_OF_SORTS the number of sorts implemented in * this file. This is defined in the * beginning of the program for * convenience. * sortName[] array holding the names of the sorts * array[] the array to be sorted passed by the * user. * a[], b[], c[] subarrays used by Merge Sort. * Basically, a[] and b[] are the smaller * subarrays, and c[] is a[] merged with * b[]. * size size of the array passed by the used. * This is very useful, because it allows * for easy memory allocation instead of * dynamic allocation. * sort number corresponding to the sort that * the user wants to use. * i,j,k indicy used by Merge Sort to move * around the array. i and j are also * used by Quick Sort for the same * purpose. * m,n the starting and ending element number * of the array used by Quick Sort. In * that sort i and j initially depend on * m and n, and serve similar purposes. * lengthOfA, lengthOfB the size of two subarrays that are * passed to Merge function to be joined * together. * pos Merge Sort's looping variable * halfLength splitting place of the subarrays in * MergeSort * workSpace a temporary array created in MergeSort * passed around to merge the subarrays. * truncatingVar an integer which makes sure that the * array element value is an integer. * pivot the pivot element chosen by QuickSort * as a key. * temp temporary variable used to swap * array values. * position, index variables used by Insertion Sort to * move around the array. * value current value being compared in * Insertion Sort. * * * * Inputs and Output: * ------------------ * The inputs of this file are provided by the user from a large data * file. I used one located in ~cs113/F96/inlcude/data*. The inputs, * however, can be any array of integers. The program also requires the * size of the array to be passed in to allocate the right amount of * memory. * The program outputs a sorted array. It does not print anything * out, just sorts the array passed in. * * * Algorithym Notes: * ----------------- * * * Merge Sort and Quick Sort both run in O(n log n), and the insertion * sort runs in O(n^2). However, since Quick Sort and Merge Sort are * recursive, and call on themself many times, their actual run time on * this computer is pretty slow. That is because of the fact the it takes * a long time for this computer to create a new function. Insertion * Sort on the other hand only uses two loops, which are relatively quick, * and even though it has to move through all the records one by one, it * ends up being the quickest of all the sorts. It is also a much shorter * function, and it does not take up too much memory, like Merge Sort. This * actually also slows Merge Sort and Quick Sort, which create a lot of * subarrays. All of this takes time. * * Algrithyms themselves of the sorts are fairly simple. Insertion Sort * moves record by record through the array, and when it finds an element * that has to be switced, it does so and uses a new key element. Merge * Sort splits the given array in smaller and smaller pieces, dividing in * two each time, and eventually just merges together a lot of unary arrays. * Quick Sort does the same, except it splits the array according to a pivot * element, and separates two kinds of data, one on each side of the pivot. * * Errors: * ------- * * There are no known errors at this point. * * Possible Enhancements: * ---------------------- * * The sorts here could be more general. For example, the could ask for * the key to sort by. Also, right now all of these sorts are only able to * sort arrays of integers, but there exist many other types of data that * needs to be sorted. * Quick Sort also could be enhanced. For example, it is very fast when * handling large amounts of data, but not that efficient when handling * small arrays. Thus, quick sort could be used to sort up to some point, * and then other sort could be used to finish sorting the small array. * Also, selecting the first element as the pivot element is not a good * idea. It would be much better to take for example the first, middle and * last integer, and find the average of them and use that as the pivot. * Also, quick sort processes all subarrays in the same order. However, * I already stated that it is not very efficient when using a small array. * So whenever a small array is encountered, it would be nice to process it * first so that the amount of time that quick sort calls itself is * minimized. * In general, recursion is not a good idea because the compiler takes a * long time to allocate memory for new arguments and local variables, * and has to save the current work when invoking a new function. * * * Comments: * --------- * The function prototypes for IntSort, NumOfIntSorts and IntSortNames * are located in hw8-sorts.h. This file is not going to be submitted. * ***************************************************************************/ #include #include #include "hw8-sorts.h" #define NUMBER_OF_SORTS 3 /* ------------------------- Function Prototypes --------------------------*/ /* * Function: Merge * Usage: Merge(subArray1, length1, * subArray2, length2, * finalArray); * Comments: Merges two already-sorted arrays into * a single sorted array. */ static void Merge(int a[], int lengthOfA, int b[], int lengthOfB, int c[]); /* * Function: MergeSort (Recursive version) * Usage: MergeSort(array, length); * * Comments: Sorts an array using a divide and conquer strategy. * */ static void MergeSort(int array[], int size); /* * Function: QuickSort * Usage: QuickSort(array, startingValue, endingValue); * * Comments: Sorts an array dividing it according to a pivot element * */ static void QuickSort(int array[], int m, int n); /* * Function: Partition * Usage: Partition(array, pointerToFirstValue, pointerToLastValue); * * Comments: Partitions the array so that all integers smaller than the pivot * element are in one subarray, and all greater than the pivot * element are in a second subarray. * */ static void Partition(int array[], int *i, int *j); /* * Function: InsertionSort * Usage: InsertionSort(array, size); * * Comments: Sorts an array going comparing integer after integer through the * whole array. * */ static void InsertionSort(int array[], int size); /* --------------------------- Main Program ------------------------------- */ int NumOfIntSorts(void) { return (NUMBER_OF_SORTS); /* this number is defined above */ } /* closes the NumOfIntSorts function */ string IntSortName(int sortNum) { string sortName[NumOfIntSorts()+1]; /* set up an array of names of implemented sorts */ /* fill up the array with sort names */ sortName[0] = "Merge Sort"; sortName[1] = "Quick Sort"; sortName[2] = "Insertion Sort"; sortName[3] = "No Sort Performed"; /* return the name of the desired sort */ if ((0 <= sortNum) && (sortNum < NumOfIntSorts())) { return (sortName[sortNum]); } else { return (sortName[3]); } } /* closes the IntSortName function */ void IntSort(int sort, int array[], int size) { /* Do this only if sortNumber matches the number of implemented sorts */ if ((0 <= sort) && (sort < NumOfIntSorts())) { switch (sort) { /* choose the right sorting method */ case 0: { /* Merge Sort Implementation */ MergeSort(array, size); break; } /* close the Quick Sort case */ case 1: { /* QuickSort Implementation */ QuickSort(array, 0, size-1); break; } /* close the Merge Sort case */ case 2: { /* Insertion Sort Implementation */ InsertionSort(array, size); break; } /* close the Insertion Sort case */ default: break; } /* close the switch statement */ } /* closes the if statement */ } /* closes the IntSort function */ /* --------------Sorting Function Definitions: MergeSort------------------- */ static 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++]; } /* end of Merge Function */ static void MergeSort(int array[], int size) { int pos; /* Looping variable */ int halfLength; /* Half the length of the array. */ int *workSpace; /* Temporary space to work in. */ if (size <= 1) return; halfLength = (size / 2); /* Point at which to split array. */ MergeSort(array, halfLength); /* Recursive calling */ MergeSort(array + halfLength, size-halfLength); workSpace = (int *) calloc(size, sizeof(int)); /* Allocate space for the local array */ Merge(array, halfLength, /* Merge two sorted subarrays */ array + halfLength, size-halfLength, workSpace); for (pos = 0; pos < size; pos++) array[pos] = workSpace[pos]; free(workSpace); } /* closes MergeSort Function */ /*----------------------- End MergeSort Functions --------------------------*/ /* ------------------------ QuickSort Function -----------------------------*/ static void QuickSort(int array[], int m, int n) { int i, j; if (m < n) { i = m; j = n; /* Initially i and j point to the first and last items */ Partition(array,&i,&j); /* partitions the array into two subarrays */ QuickSort(array,m,j); /* recursive calling */ QuickSort(array,i,n); } /* end of the if statement */ } /* end of QuickSort function */ static void Partition(int array[], int *i, int *j) { int pivot, temp, truncatingVar; truncatingVar = ( *i + *j ) / 2; /* used to truncate rationals */ pivot = array[ truncatingVar ] ; /* choose the middle key as the pivot */ do { while (array[*i] < pivot) (*i)++; /* Find leftmost i such that array[i] >= pivot. */ while (array[*j] > pivot) (*j)--; /* Find rightmost j such that array[j] <= pivot. */ if (*i <= *j) { /* if i and j didn't cross over one another, swap */ temp = array[*i]; /* switches the values */ array[*i] = array[*j]; array[*j] = temp; (*i)++; /* move i one space right */ (*j)--; /* move j one space left */ } /* closes the if statement */ } while (*i <= *j); /* while the i and j pointers haven't crossed yet */ } /* closes QuickSort */ /* --------------------- End of QuickSort Function --------------------------*/ /* ----------------------------Insertion Sort--------------------------------*/ static void InsertionSort(int array[], int size) { int position, index, value; /* position and index are used to move around the array; value is the current value being compared */ /* outer loop over all records (except first ) */ for (position = 1; position < size; position++) { value = array[position]; /* the record to be inserted */ /* inner loop over previously sorted records to find place for insertion of new records */ for (index = position; index > 0 && array[index-1] > value; index--) array[index] = array[index-1]; array[index] = value; /* records have been swithed */ } /* end of the outer loop */ } /* end of function */ /* --------------------- End of InsertionSort Function ----------------------*/ /* ------------------------ End Of File: hw8-sorts.c ----------------------- */ -- END OF LOG FILE FOR samek --