Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have a working quicksort and a randomized quicksort. How can I increase my runtime efficiency. public class A3 { //TODO: document this method public
I have a working quicksort and a randomized quicksort. How can I increase my runtime efficiency.
public class A3 { //TODO: document this method public static void quickSort(Coord[] destinations) { quicksort(destinations, 0, destinations.length-1); } private static void quicksort(Coord[] destinations, int low, int high) { int i= low; int j= high; double pivot = (destinations[i+(j-i)/2].getDist()); while(i <= j){ while(destinations[i].getDist() < pivot){ i++; } while(destinations[j].getDist()< pivot){ j--; } if (i <= j){ exchange(destinations, i,j); i++; j--; } } if(low< j){ quicksort(destinations, low,j); } if(i < high){ quicksort(destinations,high,i); } } private static void exchange(Coord[] destinations, int i, int j) { Coord temp = (destinations[i]); destinations[i] = destinations[j]; destinations[i] = temp; } //TODO: document this method public static void randQuickSort(Coord[] destinations) { randomizedQuickSort(destinations, 0, destinations.length-1); } public static void randomizedQuickSort(Coord[] destinations, int startIndex, int endIndex ){ if(startIndex < endIndex){ double pivot = randomizedPartition(destinations, startIndex,endIndex); randomizedQuickSort(destinations,startIndex, (int) (pivot-1)); randomizedQuickSort(destinations, endIndex , (int) (pivot+1)); } } public static double randomizedPartition(Coord[]destinations,int startIndex, int endIndex){ int randomEndIndex = randomNumberBetween(startIndex,endIndex); swap(destinations, endIndex,randomEndIndex); return partition(destinations,startIndex,endIndex); } public static int randomNumberBetween(int startNumber, int endNumber){ return (int)Math.floor(Math.random() * (endNumber - startNumber +1 ) + startNumber); } public static double partition(Coord[] destinations, int startIndex,int endIndex){ Coord pivotValue = destinations[endIndex]; int pivotIndex = startIndex; for (int j= startIndex; j < endIndex; j++){ if (destinations[j].getDist() <= pivotValue.getDist()){ swap(destinations,pivotIndex,j); pivotIndex = pivotIndex +1; } } swap(destinations,pivotIndex,endIndex); return pivotIndex; } public static void swap(Coord[]destinations,int firstIndex, int secondIndex){ Coord temp = (destinations[firstIndex]); destinations[firstIndex] = destinations[secondIndex]; destinations[secondIndex] = temp; } /******************************************** * * You shouldn't modify anything past here * ********************************************/ // Call system sort with a lambda expression on the comparator public static void systemSort(Coord[] destinations) { Arrays.sort(destinations, (a, b) -> Double.compare(a.getDist(), b.getDist())); } // Insertion sort eventually sorts an array public static void insertionSort(Coord[] a) { for (int i = 1; i < a.length; i++) { Coord tmpC = a[i]; int j; for (j = i-1; j >= 0 && tmpC.getDist() < a[j].getDist(); j--) a[j+1] = a[j]; a[j+1] = tmpC; } } private static Coord getOrigin(Scanner s) { double lat = s.nextDouble(); double lon = s.nextDouble(); Coord ret = new Coord(lat, lon); return ret; } private static Coord[] getDests(Scanner s, Coord start) { ArrayList<Coord> a = new ArrayList<>(); while (s.hasNextDouble()) { a.add(new Coord(s.nextDouble(), s.nextDouble(), start)); } Coord[] ret = new Coord[a.size()]; a.toArray(ret); return ret; } private static void printCoords(Coord[] a) { for (int i = 0; i < a.length; ++i) { System.out.println(a[i]); } System.out.println(); System.out.println("Paste these results into https://www.darrinward.com/lat-long/ if you want to visualize the coordinates"); } public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.printf("Enter the sorting algorithm to use [i]nsertion sort, [q]uicksort, [r]andomized quicksort, or [s]ystem quicksort): "); char algo = s.next().charAt(0); System.out.printf("Enter your starting coordinate in \"latitude longitude\" format as doubles: (e.g. 42.3366322 -71.0942150): "); Coord start = getOrigin(s); System.out.printf("Enter your end coordinates one at a time in \"latitude longitude\" format as doubles: (e.g. 38.897386 -77.037400). End your input with a non-double character:%n"); Coord[] destinations = getDests(s, start); s.close(); switch (algo) { case 'i': insertionSort(destinations); break; case 'q': quickSort(destinations); break; case 'r': randQuickSort(destinations); break; case 's': systemSort(destinations); break; default: System.out.println("Invalid search algorithm"); System.exit(0); break; } printCoords(destinations); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started