Question: i am trying to perform this on another set if numbers (the numbers i commented out in the main class) but i am still getting

i am trying to perform this on another set if numbers (the numbers i commented out in the main class) but i am still getting an out of bound exeption
public class DataStructuresLab_4 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
QuickSelect quickSelect = new QuickSelect();
int[] intArray = {9,12,5,17,20,30,8};
int k = quickSelect.quickSelect(intArray, 0, 6, 4);
//********************************************8
// int[] intArray = {12,5,19,2,15,24,18,16};
// int k = quickSelect.quickSelect(intArray, 0, 7, 4);
System.out.println("The Kth smallest number is "+k);
}
public class QuickSelect {
public int quickSelect(int[] intArray,int l, int r, int k){
int s = partition(intArray,l,r);
if (s == l+k-1){
//1+k-1
return intArray[s];
}else if (s > l + k -1){
return quickSelect(intArray, l,s-1,k);
}else{
return quickSelect(intArray, s+1,r,k+l-1-s);
}
}
public int partition(int[] intArray,int l, int r){
int p = intArray[r];
int s = l;
for (int i =l ; i < r; i++){
//0 + 1
if(intArray[i] < p){
int t = intArray[s];
// intArray[i] = intArray[s];
intArray[s] = intArray[i];
intArray[i] = t;
s = s+1;
}else{
int t = intArray[r];
intArray[r] = intArray[s];
intArray[s] = t;
return s;
}
//return s;
}
return 0;
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!