Driver.java (5492B)
1 package lab10; 2 3 /* 4 * Purpose: Data Structure and Algorithms Lab 10 Problem 1 5 * Status: Complete and thoroughly tested 6 * Last update: 01/31/18 7 * Submitted: 02/01/18 8 * Comment: test suite and sample run attached 9 * @author: John Kubach 10 * @version: 2018.01.31 11 */ 12 13 import java.util.Scanner; 14 import java.util.Random; 15 16 public class Driver { 17 Scanner in = new Scanner(System.in); 18 public static void main(String[] args) { 19 int[] items; 20 int compare = 0; 21 Driver ui = new Driver(); 22 23 items = ui.getArray(); 24 ui.printArray(items); 25 26 /* Uncomment to select sorting algorithm */ 27 //ui.iterativeMergeSort(items); 28 compare += ui.QuickSort( 0, items.length - 1,compare, items); 29 ui.printArray(items); 30 System.out.println(compare); 31 32 33 } 34 35 public void iterativeMergeSort(int items[]) { 36 System.out.println("ITERATIVE MERGE SORT"); 37 int mid; 38 int end; 39 int[] tempArray = new int[items.length]; 40 int compare = 0; 41 42 for (int size = 1; size < items.length; size = (size*2)) { 43 for (int begin = 0; begin < items.length; begin+= (2*size)) { 44 mid = (begin + size); 45 end = (begin + 2 * size); 46 if (end > items.length) { 47 end = items.length; 48 } 49 compare += mergeSubArrays(begin, end, mid, items, tempArray ); 50 } 51 } 52 53 printOperations(compare); 54 printArray(items); 55 } 56 57 public int mergeSubArrays (int begin, int end, int mid, int items[], int tempArray[]) { 58 int tempBegin = begin; 59 int tempMid = mid; 60 int compare = 0; 61 if (mid >= items.length) { 62 return 0; 63 } 64 /* 65 System.out.println(begin); 66 System.out.println(mid); 67 System.out.println(end); 68 */ 69 for (int i = begin; i < end; i++) { 70 if (tempBegin == mid) { 71 tempArray[i] = items[tempMid++]; 72 } 73 else if (tempMid == end) { 74 tempArray[i] = items[tempBegin++]; 75 } 76 else if (items[tempMid] < items[tempBegin]) { 77 tempArray[i] = items[tempMid++]; 78 } 79 else { 80 tempArray[i] = items[tempBegin++]; 81 } 82 compare++; 83 } 84 for (int i = begin; i < end; i++) { 85 items[i] = tempArray[i]; 86 } 87 return compare; 88 } 89 90 91 92 public int QuickSort(int leftBound, int rightBound, int compare, int items[]){ 93 int pivot; 94 if (leftBound < rightBound) { 95 compare++; 96 System.out.println("SORTING ARRAY" ); 97 printArray(items); 98 System.out.println(); 99 pivot = quickSortPartition(leftBound, rightBound, items); 100 QuickSort(leftBound, pivot - 1, compare, items); 101 QuickSort(pivot + 1, rightBound, compare, items); 102 } 103 return compare; 104 } 105 106 public int quickSortPartition(int leftbound, int rightbound, int items[]) { 107 int temp; 108 int lessThan = leftbound; 109 int pivot; 110 System.out.println("PARTITIONING ARRAY "); 111 printArray(items); 112 System.out.println(); 113 getPivot(leftbound, rightbound, items); 114 pivot = items[leftbound]; 115 System.out.println("COMPARING ITEMS"); 116 for (int greaterThan = leftbound + 1; greaterThan <= rightbound; greaterThan++) { 117 if (items[greaterThan] < pivot) { 118 System.out.println(items[greaterThan] + " IS LESS THAN " + pivot); 119 ++lessThan; 120 System.out.println("SWAP " + items[greaterThan] + " WITH " + items[lessThan]); 121 temp = items[greaterThan]; 122 items[greaterThan] = items[lessThan]; 123 items[lessThan] = temp; 124 } 125 } 126 System.out.println("SWAP PIVOT " + items[leftbound] + " WITH LAST ITEM IN <P " + items[lessThan]); 127 temp = items[leftbound]; 128 items[leftbound] = items[lessThan]; 129 items[lessThan] = temp; 130 System.out.println(); 131 return lessThan; 132 } 133 public void getPivot(int leftBound, int rightBound, int items[]) { 134 int pivot; 135 int temp; 136 System.out.println("FINDING PIVOT OF ARRAY "); 137 printArray(items); 138 Random generatePivot = new Random(); 139 pivot = generatePivot.nextInt((rightBound-leftBound) + leftBound); 140 System.out.println("GET PIVOT IS " + items[pivot]); 141 if (leftBound <= pivot) { 142 temp = items[leftBound]; 143 items[leftBound] = items[pivot]; 144 items[pivot] = temp; 145 } 146 printArray(items); 147 System.out.println(); 148 } 149 150 public int[] getArray() { 151 int size; 152 Random num = new Random(); 153 System.out.print("Enter number of integers: "); 154 size = Integer.parseInt(in.nextLine()); 155 System.out.println("GENERATING RANDOM ARRAY..."); 156 int newArray[] = new int[size]; 157 for (int i = 0; i < newArray.length; i++) { 158 newArray[i] = num.nextInt(100); 159 } 160 return newArray; 161 } 162 163 public void printOperations(int compare) { 164 System.out.println("Number of key comparisons: " + compare); 165 } 166 167 public void printArray(int items[]) { 168 for (int i = 0; i < items.length; i++) { 169 System.out.print(items[i] + " "); 170 } 171 System.out.println(); 172 } 173 }