0% found this document useful (0 votes)
5 views

csc241.20S Sorting Methods Assignment.docx

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

csc241.20S Sorting Methods Assignment.docx

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSC 241 - Abstract Data Types and Programming Methodologies

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.

In particular, we are going to implement Insertion Sort and QuickSort. Our


implementation will be very similar to the ones presented in the lecture slides:

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.

Part 1 - Sorter Class


Open the file containing the Sorter class. This class will contain both methods:

public class Sorter {


// Insertion Sort

// QuickSort
}

Part 2 - Insertion Sort


Recall from lecture that the Insertion Sort method operates by iteratively selecting an
array element ai, then repeatedly shifting values to the right until the correct location for
that element is found. When found, ai is copied into the location. Recall also that this
process is repeated for every element, and thus the array contents are repeatedly
shifted to make space for each value. It is this repeated shifting that makes the run time
O(n2).
CSC 241 - Abstract Data Types and Programming Methodologies
Sorting Methods

Copy this code to the Sorter class:

public int insertionSort(int[] data) {


int i, j, item, count;
// initialize count
count = 0;
for (i = 1; i < data.length; i++) {
// select item to place
item = data[i];
j = i;
while (j > 0 && data[j - 1] > item) {
// continue shifting items until correct position is found
data[j] = data[j - 1];
j--;
count++; // increment shift count
}
data[j] = item; // place item in correct location
}
return count;
}

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).

Copy this method into the Sorter class:

public int quickSort(int[] data, int first, int last) {


int left, right, pivot, count;
// initialize count
count = 0;
if (first >= last) {
CSC 241 - Abstract Data Types and Programming Methodologies
Sorting Methods

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);

// return the total swap count


return count + rCount + lCount;
}
CSC 241 - Abstract Data Types and Programming Methodologies
Sorting Methods

Part 4 -- Testing
Go to the main() method of your program. Declare and initialize an integer array with
random values as follows:

int [] inputArray = new int[10];

Random r = new Random();


// initialize with random numbers
for (int i = 0; i < inputArray.length; i++) {
// Generate random int bounded by array size
inputArray[i] = r.nextInt(inputArray.length);
}

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();

// Create a Sorter object


Sorter sorter = new Sorter();
int count = 0; // Count the shift/swap operations
count = sorter.insertionSort(inputArray);
//count = sorter.quickSort(inputArray, 0, inputArray.length - 1);

// Show sorted array


System.out.println("---=== Sorted ===---");
System.out.println(arrayToString(inputArray));
System.out.println("Count: " + count);
CSC 241 - Abstract Data Types and Programming Methodologies
Sorting Methods

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.

Finally, provide your explanation of what the graphs are showing.

Create a PDF version of your report and submit it to in Blackboard

You might also like