Last active
April 24, 2020 21:44
-
-
Save zeitan/d8718389792b0f718d3ee1ef0c726a22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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