Fall, 2016 Lab #3
Total Page:16
File Type:pdf, Size:1020Kb
Texas Christian University CoSc 20803 - Fall, 2016 Lab #3 Points: 100 points Language: Java Due Date: Midnight, Thursday, Nov 17, 2016 (complete Java project and manually-generated Excel spreadsheet ZIPped together and submitted with TURNIN). Purpose: The purpose of this assignment is to allow you to evaluate the performance of several different sorting algorithms on data in various degrees of disorder. Processing: You should implement the following sorting algorithms and gather statistics as noted below: Method #1 Method #2 Method #3 Method #4 Bubblesort Cocktail Shaker Sort Shell Sort Quick Sort (pp. 230-232) (discussed in class) (discussed in class) (pp. 232-236) Input: Three input data sets will be provided (each of which contains 5000, 4-byte alphanumeric keys). The files are: Ascending.dat, Descending.dat, and Random.dat (obtainable from the CoSc Department’s class website: GUI: For this lab you should develop a manual data collection program that employs a VERY SIMPLE graphical user interface. Your program should accommodate input from the user specifying: a) the integer number of records to be sorted, b) the name of the input file to be used as the source of data (via a JFileChooser()), and c) the sorting method to use. Your program should allow the sorting methods listed above to be performed. Statistics: You should sort, ascendingly, alphanumeric keys in each of the three data files provided, keeping statistics for each file and for each sort method. Keep track of: (i) the number of key compares (Ki:Kj) and (ii) the number of key moves (e.g., Ki <--- Kj). Note: a “swap”, Ki <---> Kj counts as 3 moves. Output: Manually create a nicely formatted Excel spreadsheet detailing (for each sorting method and for each data set): the number of keys sorted, the total number of key compares, the average number of key compares, the total number of key moves, and the average number of key moves. (Excel charts should be used to illustrate your results). To ensure that everyone’s Excel spreadsheet shows similar results, sort the following number of keys from each file for each algorithm: 100 keys, 1000 keys, 2000 keys, and 5000 keys. (i.e., sort 100 keys from the Ascending.dat file using each of the above sorting methods, then sort the same 100 keys from the Descending.dat file using each of the above methods, then sort the same 100 keys from the Random.dat file using the same sorting methods. Record your results and repeat for 1000 keys, 2000, etc. Note: You are only counting key moves and key comparisons (not comparisons to see how the value of one control variable (say in a for loop or a while loop) compares to another, etc). Sorting Algorithm Code – please use so that everyone is using the same algorithm: • BubbleSort: Bubble Sort Algorithm BubbleSort(Table t[0..n-1]); { //Sorts an n element table, t, into ascending order valuesWereInterchanged = true passCount = 1 //loop controls the number of passes while ((passCount <= (n-1)) & (valuesWereInterchanged)) { valuesWereInterchanged = false // initially no interchanges // the inner loop governs each individual pass for (j = 0; j <= (n–1)-passCount; j++) { if (t[j] > t[j+1])// elements out of order then begin // an interchange is necessary valuesWereInterchanged = true // exchange the entire record, not just the key temp = t[j] t[j] = t[j+1] t[j+1] = temp end } passCount = passCount + 1 } } end BubbleSort • Cocktail Shaker Sort: Algorithm CocktailShakerSort( Table t[0..n-1]); { //Sorts an n element table, t, into ascending order do { swapped = false for (i = 0; i <= (n–2); i++) { if (t[i] > t[i+1]) then { // elements are in the wrong order swap( t[i], t[i+1] ) swapped = true } end if } //end for if (not swapped) then // exit outer loop if no swaps occurred break //do-while loop end if swapped = false for (i = (n-2); j >= 0; i--) { if (t[i] > t[i+1]) then { swap( t[i], t[i+1] ) swapped = true } end if } //end for } while swapped // if no elements swapped, list is sorted } //end algorithm • ShellSort: (Diminishing Increment Sort) Algorithm ShellSort(Table R[0 .. n-1], int h[0 .. t-1]); { //Sorts an n element table, t, into ascending order. //Input: an array R that holds the records to be sorted, //an array h that holds the increments such that: h[0] = 1 and //h[i] > h[i-1]. //Use Shell’s increments: //hi = n/2, hi-1 = hi/2, hi-2 = hi-1/2 ... h1 = 1 //Example (for n = 900; h9 = 900/2 = 450, h8 = 450/2 = 225, // h7 = 225/2 = 112, h6 = 112/2 = 56, . ., h1 = 3/2 = 1 //FirstInc returns the largest increment to be used when //sorting the table inc = FirstInc(h) while (inc >= 1) { for i = inc to (n-1) { j = i K = key[i] X = R[i] while ((j >= inc) & key[j-inc] > K) { R[j] = R[j-inc] j = j – inc } R[j] = X } //NextInc returns the next increment smaller than inc inc = NextInc(h) } } end ShellSort • QuickSort: Algorithm QuickSort(Table[] t, int left, int right); { //Sorts an n element table (t[left], … t[right]) into //ascending order. The initial invocation is as: //QuickSort(t, 0, (n-1)) if (left < right) then { // i is used to scan from the left to find elements >= // the pivot key i = left // j is used to scan from the right to find elements <= // the pivot key j = (right + 1) // set the pivot element pivot = t[left] while (i < j) { i = i + 1 while ((i <= right) & (t[i] < pivot)) i =i + 1 j = j - 1 while ((j >= left) & (t[j] > pivot)) j = j – 1 if (i <= right) t[i] <==> t[j] } // move the pivot element into its proper position t[j] <==> t[left] QuickSort(t, left, j-1) //QuickSort low order partition QuickSort(t, j+1, right)//QuickSort high order partition } } .