I mainly do python because its in my school syllabus, but I enjoy java(I am still fairly new to it). I was trying to make a quick sort program(recursive) in java but a typical stackoverflow occurs.I've made recursove program in java but this time i just don't know what is wrong
This is the function for setting pivot:
static void part(int[]a){
int n = a.length;
j=-1;
for(int i=0;i<n;i++){
if(a[n-1]<a[i]){continue;}
else if(a[n-1]>=a[i]){
j++;
if(a[j]>a[i]){
int t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
This is the function that sorts:
static int[] sort(int[]a){
if(a.length<=1){
return(a);
}
part(a);
int temp=j; //Variable 'j' is public
sort(slice(a,0,temp));
sort(slice(a,temp+1,a.length+1));
return(a);
}
This is the slice function
static int[] slice(int []a, int b, int c){
int[]temp=new int[c-b+2];
int ind=0;
for(int i=b;i<c;i++,ind++){
temp[ind]=a[i];
}
return(temp);
}
I've tried changing the value for bounds but always the error occurs.
static int[] slice(int []a, int b, int c){` int[]temp=new int[c-b+2];`int ind=0;`for(int i=b;i<c;i++,ind++){ temp[ind]=a[i]Arrays.copyOfRange())jan instance variable instead of just havingpartreturn the pivot?slicecreates a copy of the array. Butsortreturns the original array. If you're recursively sorting copies of the original array, how do you expect to sort the original array?