Question
Sorting Objects with the Quicksort Algorithm The IntQuickSorter class presented in this chapter sorts an array of int values. Create an ObjectQuickSorter class that can
Sorting Objects with the Quicksort Algorithm The IntQuickSorter class presented in this chapter sorts an array of int values. Create an ObjectQuickSorter class that can sort Comparable objects. Demonstrate the class in a program that sorts an array of String objects. (java)
intquicksorter class in java
public class quicksort { public static void quickSort(int[] arr) { doQuickSort(arr,0,arr.length-1); }
private static void doQuickSort(int a[], int start, int end) { int pivot;
if(start < end) { pivot = partition(a,start,end);
doQuickSort(a,start,pivot - 1);
doQuickSort(a, pivot + 1, end); } }
private static int partition(int ar[], int start, int end) { int pivotval,endoflist,mid;
mid = (start + end) / 2;
swap(ar,start,mid);
pivotval = ar[start];
endoflist = start;
for(int i = start + 1; i <= end; i++) { if(ar[i] < pivotval) { endoflist++; swap(ar,endoflist,i); } }
swap(ar,start,endoflist);
return endoflist; }
private static void swap(int[] array, int a, int b) { int temp; temp = array[a]; array[a] = array[b]; array[b] = temp; } }
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