Requirements: Compare the efficiency of Selection Sort, Insertion Sort and Quicksort.

Approach: Starting from your second homework assignment, evaluate the efficiency of Selection Sort, Insertion Sort and Quicksort. For doing this, you should evaluate their corresponding implementations in each of the 3 cases (best, worst, and average) and count the number of operations performed (assignments, comparisons, and overall, separately).
Selection Sort code:
// Method: selectionSort
// Author: instructorX
// Date: dd/mm/yyyy
// Purpose: A method that sorts an array using a selection sort

public static void selectionSort(Comparable[] array)
int i;
for (i = 0; i < array.length; i++) { int minIndex = i; for (int j = i + 1; j < array.length; j++) if (array[j].compareTo(array[minIndex]) < 0) minIndex = j; swap(array, minIndex, i); } } Insertion Sort code: // Method: insertionSort // Author: instructorX // Date: dd/mm/yyyy // Purpose: A method that sorts an array using an insertion sort public static void insertionSort(Comparable[] array) { for (int i = 1; i < array.length; i++) { Comparable temp = array[i]; int j = i - 1; while (j >= 0 && temp.compareTo(array[j]) < 0) { array[j + 1] = array[j]; j--; } array[j +1 ] = temp; } } QuickSort Code: public static void quickSort(int [] items, int leftIndex, int rightIndex) { int i, j, temp, pivotValue, partitionSize; partitionSize = rightIndex - leftIndex + 1; if(partitionSize <= 1) // base case, one item to be sorted return; pivotValue = items[(leftIndex + rightIndex) / 2]; i = leftIndex; // initialize the two partition indices j = rightIndex; // look for items in wrong partitions and switch them do { while (items[i] < pivotValue) // left item is in correct partition i++; while (items[j] > pivotValue) // right item is in correct partition
if (i <= j) // pointers have not crossed, switch items { temp = items[i]; items[i] = items[j]; items[j]=temp; i++; j--; } } while (i <= j); // the pointers have not crossed // reduced problems quickSort(items, leftIndex, j); // sort left partition, quickSort(items, i, rightIndex); // sort right partition } For counting them, you need to add in the right place specific statements to increment the counters for assignments and/or comparisons, wherever needed (actually you have to add for Quicksort only, in case you received no feedback for review/update/change the counters from the second homework assignment). Draw charts to show how the running time (estimated in numbers of assignments A(n), comparisons C(n), and overall time T(n)=A(n) + C(n)) growths as the size of the input data n is growing. To draw the charts, vary the input size (n) between 100 and 1000, with an increment of maximum 100. For each size, generate the appropriate input sequence (best, worst, and average) for the specific sorting method (be aware: best/worst cases are not necessary the same for the three algorithms), run it, and store the values (A(n), C(n), T(n)). For the average case, you have to repeat the measurements m times (m=5 should suffice) and report their average. Moreover, for the average case, make sure you always use the same input sequence for all three sorting methods for a fair comparison. For each of the analyzed cases, generate charts which compare the three methods; use different charts for the number of comparisons, number of assignments and total number of operations. Name your charts and curves on each chart appropriately (that is, specify what the chart/curve represents). Feedback on Second Homework Assignment: Your solution fails to display an i/o sequence to show the algorithms are sorting. It was requested “to show your algorithms are sorting (to display the input and output sequences)” Indexes shouldn’t be counted. Reason: the indexes are integers, and operations on integers are performed much faster than operation on other types of data – and in the real world scenarios, the data is some structured data, with several components, and we first have to reach to the item (the key, which is not necessarily an int) based on which the sorting is performed and do the comparison and the rest of the task. In selection sort the comparison (array[j].compareTo(array[minIndex]) < 0) is done no matter what’s its result (true or false). Hence, it should have been counted outside the conditional statement (that is in both braches, then and else, not just in one branch). In insertion sort the comparison (temp.compareTo(array[j]) < 0) is done once more, by the time it is evaluated to false, and the execution exits the while loop. Why so many arrays? Just 2 of the max size is enough to run all the tests! Diagrams: please group them by case and by measure (on separate charts) as it is difficult to follow when so many curves are on the same diagram (no penalty on this – this time). Deliverables: You should submit (1) all the source (.java) files, (2) an output sample (screenshot showing program execution and the results of your testing, to show the methods are actually sorting) (3) the diagrams (the easiest way to draw them is by using Microsoft Excel Worksheet) and (4) a document file describing your solution. The solution description document should include the following elements: justification for the added statements (the ones that increment the counters), the choice for the input data for best and worst cases for each algorithm, with justification, the interpretation of the diagrams and lessons learned.

New Download