Skip to content

Instantly share code, notes, and snippets.

@zeitan
Last active April 24, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeitan/d8718389792b0f718d3ee1ef0c726a22 to your computer and use it in GitHub Desktop.
Save zeitan/d8718389792b0f718d3ee1ef0c726a22 to your computer and use it in GitHub Desktop.
private static int findKthSmallestNumber3(int[] data, int low, int high, int k) {
if (k > 0 && k <= high - low + 1) {
int partitionCalculated = partition(data, low, high);
if (partitionCalculated - 1 == k -1 ) {
return data[partitionCalculated - 1 ];
}
if (partitionCalculated - 1 > k -1) {
return findKthSmallestNumber3(data, low, partitionCalculated - 1, k);
}
return findKthSmallestNumber3(data, partitionCalculated + 1, high, k - partitionCalculated + low - 1);
}
return Integer.MAX_VALUE;
}
private static int findKthSmallestNumber(int[] numbers, int k) {
int[] acums = new int[numbers.length];
int length = numbers.length;
for (int i = 0; i < length; i++) {
for(int j= i + 1; j < length; j++) {
if (numbers[i] >= numbers[j]) {
acums[i] = acums[i] + 1;
}
else {
acums[j] = acums[j] + 1;
}
}
if (acums[i] == k - 1) {
return numbers[i];
}
}
return Integer.MAX_VALUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment