Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class DataStructures_7 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here
public class DataStructures_7 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
QuickSort quickSort = new QuickSort();
int[] intArray = {5,3,1,9,8,2,4,7};
quickSort.quickSort(intArray,0, 7);
for(int i = 0; i
System.out.println(intArray[i] + " ");
}
//System.out.println("The answer after quick sort is " + k );
}
public class QuickSort {
public void quickSort(int[] intArray, int l, int r){
int s = hoarePartition(intArray , l , r);
if(l
quickSort(intArray, l, s-1);
quickSort(intArray, s + 1, r);
}
}
public int hoarePartition(int[] intArray, int l, int r){
int p = intArray[l];
int i = l + 1 ;
int j = r ;
while(l ALGORITHM Quicksort(A[l..r )) in ito //Sorts a subarray by quicksortid .) //Input: Subarray of array A[O..n-1), defined by its left and right // indices I andr //Output: Subarray A[...] sorted in nondecreasing order JY UIT1010 fl
while(intArray[i]
i = i + 1;
}
while(intArray[j] > p){
j = j - 1;
}
int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
}
WHEN I TRY TO RUN IT IT GIVES AN ERROR
if(i >= j){
int temp = intArray[i];
intArray[i] = intArray[j];
intArray[j] = temp;
temp = intArray[l];
intArray[l] = intArray[j];
intArray[j] = temp;
}
return j;
}
}
this is quick sort using hoares partition
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