Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i am still getting an arrayIndexoutofboundsexception */ public class QuickSelect { public int quickSelect(int[] intArray,int l, int r, int k){ int s = partition(intArray,l,r); if
i am still getting an arrayIndexoutofboundsexception
*/
public class QuickSelect {
public int quickSelect(int[] intArray,int l, int r, int k){
int s = partition(intArray,l,r);
if (s == 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+1-1-s);
}
}
public int partition(int[] intArray,int l, int r){
int p = intArray[r];
int s = l;
for (int i = 0 + 1; i
if(intArray[i]
tructures 20 public eldoo * @param args the command line arguments Structures.ab_4 Source Packages datastructureslab_4 DataStructures ab_4.java L QuickSelect.java Test Packages Libraries Test Libraries software Architecture Employee OO public static void main(String[] args) { QuickSelect quickSelect = new Quickse int[] intArray = {9,12,5,17,20,30,8); int k quickSelect.quickSelect (intas System.out.println(k): 33) Output - Data StructuresLab_4 (run) Selec. selecceselect 30 Selecteeksels Stsee INNO * O Exception in thread"main" java.lang. ArrayIndexOutOfBounds Exception: -1 at datastructureslab 4. QuickSelect partition (QuickSelect Java:38) at datastructureslab 4. QuickSelec.quickSelect (QuickSelect java:26) at datastructureslab_4. Quick Select.quickSelect (QuickSelect.java:30 at datastructureslab_4.QuickSelect quickSelect (QuickSelect.java:32 at datastructureslab 4. QuickSelect quickSelect (QuickSelect.java:30) at datastructureslab 4. QuickSelect.quickSelect (Quickselect.java:32 at datastructureslab 4. QuickSelect quickseleet QuickSelect.java:32 at datastructureslab_4. QuickSelect quickseleet Qui ekselect.java:32 at datastructureslab_4.Datassructureslab mainData StructuresLab_4.h:22 e o Output Type here to search public class DataStructures Lab_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); System.out.println(k): ayIndexOutot Bounds Estception: -1 de partition (Quick Selectva: 38 et qui Select (Quickselede avara co.uickSelect Quick Belect.ava: 3 LexSelect Quickselect. od quickSelect Quick Seles Seleco Qudok 561 Co.quickSelect Quicksel coquie Seleci Quie:selee L 11 12 public class Quick Select public int quickSelect (int[] intArray, int i, int i, int k) { int s = partition (intArray,1,r); if (s - 1+k-1){ return intArray[s]; }else if (s > 1 + k-1) { return quickSelect (intArray, 1,3-1,k); Jelse { return quickSelect (intArray, 3+1,1, k+1-1-3); public int partition (int[] intArray, int i, int r) { int p = intArray[r]; int s = 1; for (int i = 1 + 1; i 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 0;
}
}
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);
System.out.println(k);
}
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