public class FindMinKth {
public static int minKth2(int[] array, int k){
int[] arr = copyArray(array);
return process2(arr,0,arr.length - 1, k-1);
}
public static int process2(int[] arr, int L, int R, int index){
if (L == R){
return arr[L];
}
int pivot = arr[L + (int)(Math.random() * (R - L + 1))];
int[] range = partition(arr, L, R, pivot);
if(range[0] <= index && index <= range[1]){
return arr[index];
}else if(index < range[0]){
return process2(arr, L, range[0]-1, index);
}else{
return process2(arr,range[1]+1, R, index);
}
}
public static int[] partition(int[] arr, int L, int R, int pivot){
int less = L - 1;
int cur = L;
int more = R + 1;
while(cur < more){
if(arr[cur] < pivot){
swap(arr, ++less, cur++);
}else if(arr[cur] > pivot){
swap(arr,cur, --more);
}else{
cur++;
}
}
return new int[]{less+1, more-1};
}
public static void swap(int[] arr, int i1, int i2){
int temp = arr[i2];
arr[i2] = arr[i1];
arr[i1] = temp;
}
public static int[] copyArray(int[] arr){
int[] ans = new int[arr.length];
for (int i = 0; i < ans.length; i++) {
ans[i] = arr[i];
}
return ans;
}
public static int[] generateRandomArray(int maxSize, int maxValue){
int[] arr = new int[(int)(Math.random()*maxSize) + 1];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int)(Math.random() * (maxValue + 1));
}
return arr;
}
/*
* bfprt算法
* 1. 5个数一组,排序
* 2. 把每组的中位数拿出来,再组成一个数组arr
* 3. 取出arr数组的中位数p
*/
public static int minKth3(int[] array, int k) {
int[] arr = copyArray(array);
return bfprt(arr,0,arr.length-1,k-1);
}
public static int bfprt(int[] arr, int L, int R, int index){
if(L == R){
return arr[L];
}
int pivot = medianOfMedians(arr, L, R);
int[] range = partition(arr, L, R, pivot);
if(range[0] <= index && index <= range[1]){
return arr[index];
}else if(index < range[0]){
return bfprt(arr, L, range[0]-1, index);
}else{
return bfprt(arr,range[1]+1, R, index);
}
}
// 取出中位数的中位数
public static int medianOfMedians(int[] arr, int L, int R){
int size = R - L + 1;
int offset = size % 5 == 0 ? 0 : 1;
int[] mArr = new int[size/5 + offset];
for (int i = 0; i < mArr.length; i++) {
int teamFirst = L + i * 5;
mArr[i] = getMedian(arr, teamFirst, Math.min(R, teamFirst + 4));
}
return bfprt(mArr,0,mArr.length-1,mArr.length/2);
}
public static int getMedian(int[] arr, int L, int R){
// 插入排序
insertionSort(arr, L, R);
return arr[(R+L)/2];
}
public static void insertionSort(int [] arr, int L, int R){
for (int i = L+1; i <= R; i++) {
for (int j = i-1; j >=L && arr[j] > arr[j+1]; j--) {
swap(arr,j,j+1);
}
}
}
public static void main(String[] args) {
int testTime = 100;
int maxSize = 20;
int maxValue = 100;
for (int i = 0; i < testTime; i++) {
int[] arr = generateRandomArray(maxSize,maxValue);
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[j]+" ");
}
System.out.println();
int k = (int)(Math.random() * arr.length)+1;
int[] arr2 = copyArray(arr);
int a1 = minKth2(arr,k);
int a2 = minKth3(arr2,k);
if (a1 != a2){
System.out.println("=======false=====");
break;
}
System.out.println(a1);
System.out.println("===="+k);
System.out.println(a2);
System.out.println("====================="+k+"====="+i);
}
}
}