rendered paste body static int findRankK (int k, int[] L) throws Exception {
if (L.length == 1) {
return L[0];
}
int pivot = L[0];
int L1length = 0;
int L2length = 0;
int L3length = 0;
for (int i = 0; i < L.length; i++) {
if (pivot == L[i]) {
L2length++;
continue;
}
else if (pivot < L[i]) {
L3length++;
continue;
}
else if (pivot > L[i]) {
L1length++;
continue;
}
else {
throw new Exception ("error");
}
}
int[] L1 = new int[L1length];
int[] L2 = new int[L2length];
int[] L3 = new int[L3length];
int L1counter = 0;
int L2counter = 0;
int L3counter = 0;
for (int i = 0; i < L.length; i++) {
if (pivot == L[i]) {
L2[L2counter] = L[i];
L2counter++;
continue;
}
else if (pivot < L[i]) {
L3[L3counter] = L[i];
L3counter++;
continue;
}
else if (pivot > L[i]) {
L1[L1counter] = L[i];
L1counter++;
continue;
}
else {
throw new Exception ("error");
}
}
if (k < L1length) {
return findRankK(k,L1);
}
else if (k < (L1length+L2length)) {
return pivot;
}
else {
return findRankK(k-L1length-L2length,L3);
}
}