找到一组数据的第K大元素
This commit is contained in:
parent
2c6b918020
commit
c486abb37a
51
java/12_sorts/KthSmallest.java
Normal file
51
java/12_sorts/KthSmallest.java
Normal file
@ -0,0 +1,51 @@
|
||||
package sort;
|
||||
|
||||
/**
|
||||
* @author wangjunwei87
|
||||
* @since 2019-03-10
|
||||
*/
|
||||
public class KthSmallest {
|
||||
|
||||
public static int kthSmallest(int[] arr, int k) {
|
||||
if (arr == null || arr.length < k) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int partition = partition(arr, 0, arr.length - 1);
|
||||
while (partition + 1 != k) {
|
||||
if (partition + 1 < k) {
|
||||
partition = partition(arr, partition + 1, arr.length - 1);
|
||||
} else {
|
||||
partition = partition(arr, 0, partition - 1);
|
||||
}
|
||||
}
|
||||
|
||||
return arr[partition];
|
||||
}
|
||||
|
||||
private static int partition(int[] arr, int p, int r) {
|
||||
int pivot = arr[r];
|
||||
|
||||
int i = p;
|
||||
for (int j = p; j <= r - 1; j++) {
|
||||
if (arr[j] < pivot) {
|
||||
swap(arr, i, j);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
swap(arr, i, r);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
private static void swap(int[] arr, int i, int j) {
|
||||
if (i == j) {
|
||||
return;
|
||||
}
|
||||
|
||||
int tmp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = tmp;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user