trying to implement quicksort...
in
Programming Questions
•
7 months ago
So I'm trying to implement quicksort method that was originally from here:
but I get an error. ArrayIndexOutOfBoundsException: 0
Here's the code for the sort:
int[] QuickSort( int[] data){
int pivot = 0;
int len = data.length;
int ind = len / 2;
int i;
int j = 0;
int k = 0;
int[] L = new int[data.length];
int[] R = new int[data.length];
int[] sorted = new int[data.length];
pivot = data[ind]; // the error lies in this line of code. when the method is called as the program runs, it gives the error // and highlights this line
for(i = 0;i<data.length;i++){
if(i!=ind){
if(data[i]<pivot){
L[j] = data[i];
j++;
}else{
R[k] = data[i];
k++;
}
}
}
int[] sortedL = new int[j];
int[] sortedR = new int[k];
arrayCopy(L,0,sortedL,0,j);
arrayCopy(R,0,sortedR,0,k);
sortedL = QuickSort(sortedL);
sortedR = QuickSort(sortedR);
arrayCopy(sortedL, 0, sorted, 0, j);
sorted[j] = pivot;
arrayCopy(sortedR, 0, sorted, j+1, k);
return sorted;
}
If someone has an idea as to why this happens or even a better method of quicksorting in processing, I would greatly appreciate it
1