Read: Read Chapters in the class text, 8, 11, 13 new edition (or Chapters 8, 10 and 15 old edition) and the the class notes on Loop Invariants.
Program: You will write two sorting programs, InsertionSort.C and QuickSort.C and use a suite of increasing sized inputs show how the run time varies with input size for each of the two programs. You are required to submit a plot of your results to complete the assignment.
Your program development must include statement of Loop Invariants for each looping construct in your code. See the class notes on Loop Invariants.
In detail: your program opens a file, reads the number of integers to follow as the first integer in the file, and inputs that many integers, one integer per line from the file. The program places each integer in an array location. There will be no more than 20,000 integers in the file. There is an example of how to input integers from this format of file.
The InsertionSort.C program will sort the numbers in the array by using a while loop whose Loop Invariant is:
Each time through the loop, increase i and restore the loop invariant by placing the new item in its proper place among the other items. Do this by swaping the integer with the integer just before it in the array if they are not in ascending order. You may have to got just until the the integer is in location 0. Overlook this possibility and your program will have a bug.Then print the array (if it is small), or just the word DONE if the array is large.
The QuickSort.C program acts the same except that Quick Sort is used to sort the numbers. Quick Sort is recursive. It receives an array A and two indices, top and bottom,
and sorts the array items between top and bottom, including top but not bottom. Sorting the array of n elements sets top to 0, bottom to n.To sort A[top] .. A[bottom-1]:
You create test input files using the program MakeNumbers.C. Download the source, CC and run the file. It will prompt you for a file name (e.g. input.dat) and the number of integers to generate. Run both programs for time on sample inputs of various sizes, to see how run time varies with size. To time your program, use the Unix command time followed by the command name (a.out), on the same line:
appomattox> time a.out Sorting 20000 integers. Checking. 0.192u 0.041s 0:00.24 95.8% 0+0k 0+0io 0pf+0w appomattox>
Insertion Sort is a O(n^2) algorithm: it should run in time proportional to the number of elements to sort squared. I.e., double the input, quadruple the run time. Quick Sort is an O(n log n) algorithm run slightly more than proportional to the number of elements. I.e., double the input, double (plus a constant bit) the run time.
However each algorithm demonstrates sensitivity to the type of input given. Insertion Sort is as fast as can be on already sorted data. On the contrary, already sorted data turns Quick Sort into Slowest-Sort. The study of algorithms, such as the two sorting algorithms presented in this homework assignment, is a major part of Computer Science. The course mth517 is devoted entirely to the study of algoritms.