I want to modify QuickSort (in Java) so that every time Partition is called, the median of the proportioned array is used as the pivot.
I have a median selection algorithm in Java that returns the kth smallest element, in this case the median. I have tons of quicksort algorithms in java that all work by themselves and sort an array. Unfortunately I can't combine those two in order to achieve the above... Everytime I try it i usually get stackoverflow erros.
Can anybody show me code to see how it can be done?
Thanks
EDIT: For example this is a median selection algorithm that I have tried to use.
public int quickSelect(int[] A, int p, int r, int k) {
if (p==r) return A[p];
int q = Partition(A,p,r);
int len = q-p+1;
if (k == len) return A[q];
else if (k<len) return Select(A,p,q-1,k);
else return Select(A,q+1,r,k-len);
}
public int partition(int[]A, int p, int r) {
int x = A[r];
int i = p-1;
for (int j = p; j<=r-1; j++) {
if (A[j] <= x) {
i++;
swap(A,i,j);
}
}
swap(A,i+1,r);
return i+1;
}
It works by itself but when I try to call quickSelect through quicksort's partition function to return the pivot to be used, it doesn't work. Obviously I'm doing something wrong but I don't know what. Unfortunately on the Internet I haven't found any algorithm, even in pseudocode, that would combine a median selection with quicksort.