csc241.20S Sorting Methods Assignment.docx
csc241.20S Sorting Methods Assignment.docx
Sorting Methods
In this assignment, we are going to write two sorting methods and perform an analysis
of their performance. Each one accepts an array of integers, and each sorts the
elements in increasing order. Also, each of these methods sorts the array “in place”,
meaning that no additional memory storage is needed.
http://www3.cs.stonybrook.edu/~cse214/lecture_slides/unit9.pdf
However, we are going to modify them slightly so that each returns an integer value.
This value will be a counter showing how many swaps are necessary to sort the
elements in the array.
// QuickSort
}
Notice that we have modified this method so that it counts the number of shifts that are
made, then returns the count when the method terminates. This will allow us to run the
method repeatedly and get an average count.
Part 3 -- QuickSort
We will now turn our attention to the QuickSort method. Like Insertion Sort, it can sort
an array in place. However, that is where the similarity ends. QuickSort is a “divide and
conquer” algorithm, meaning that it divides the original problem (unsorted array) into
smaller sub arrays until the size of the sub arrays becomes trivially easy to sort. Every
round of QuickSort produces two small sub arrays of half the original size. In the
average case, these techniques produce a run time of O(n log n).
return count;
}
left = first;
right = last;
// chose a pivot between the left and right
pivot = data[(first + last) / 2];
do {
// Find a left value in the wrong place
while (data[left] < pivot) {
left++;
}
// Find a right value in the wrong place
while (data[right] > pivot) {
right--;
}
// swap the values
if (left <= right) {
count++; // increment the swap count
// swap the values
int temp = data[left];
data[left] = data[right];
data[right] = temp;
left++;
right--;
}
}
while (left <= right);
// Recursively call QuickSort on the left and right sub arrays
int rCount = quickSort(data, first, right);
int lCount = quickSort(data, left, last);
Part 4 -- Testing
Go to the main() method of your program. Declare and initialize an integer array with
random values as follows:
Add the following statements that show the unsorted array, create a Sorter object, use it
to perform Insertion Sort, then show the array again (now sorted):
// Show Array
System.out.println("---=== Before ===---");
System.out.println(arrayToString(inputArray));
System.out.println();
Part 5 -- Report
Run this program repeatedly for each type of sorting algorithm and record the count for
each trial. I suggest that you use a Google spreadsheet to record the swap count for
each trial and calculate the average for each set of trials in another cell.
When choosing the size of the input array, consider doubling the size with each trail,
e.g. 10, 20, 40, 80, then 100, 200, 400, etc. The program can produce unusual results
for arrays sizes above 80K, so you can limit your testing to arrays of smaller size. You
might also consider running experiments with small arrays, i.e. 5, 2, etc. Finally, printing
all the array elements can take much longer than the actual sorting. So, consider
commenting out those statements when working with arrays that have more than 50
elements.
Your report should include a table showing all the trails (or a link to the Google sheet),
and a graph showing the swap count plotted for each algorithm.