we have a collection of comparable held in a bag and have to find the kth largest element. I copied to a hashSet to remove duplicates then converted the hash set to an array to be sorted and consequently the kth element accessed. its compiling but fails the testing, and I can't figure out whats wrong. any ideas?
public E kth(int K){
uniqueSet();
Object[] uniqueArr = hashSet.toArray();
StartQuick(uniqueArr);
return (E)uniqueArr[K-1];
}
private void StartQuick(Object[] uniqueArr){
int i = 0, j = uniqueArr.length;
quickSort(uniqueArr, 0, j);
}
private void quickSort(Object[] uniqueArr, int i, int j) {
int index = partition(uniqueArr, i, j);
if (i < index - 1)
quickSort(rankBagArr, index-1 ,j);
if (index < j)
quickSort(rankBagArr, i, index-1);
}
private int partition(Object[] uniqueArr, int i, int j){
E tmp;
E pivot = (E)rankBagArr[(i + j) / 2];
while (i <= j) {
while (rankBagArr[i].compareTo(pivot)<0)
i++;
while (rankBagArr[j].compareTo(pivot)>0)
j--;
if (i <= j) {
tmp = (E)rankBagArr[i];
rankBagArr[i] = rankBagArr[j];
rankBagArr[j] = tmp;
i++;
j--;
}
};
return i;
}