题解 | #寻找第K大#
寻找第K大
http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf
public static int findKth(int[] a, int n, int K) { // write code here QuickSort(a, 0, n - 1, K); return res; } private static void QuickSort(int[] a, int l, int r, int k) { if (l < r) { int i = l, j = r; while (i < j) { int temp = a[l]; while (i < j && a[i] < temp) i++; while (i < j && a[j] >= temp) j--; swap(a, i, j); } swap(a, l, i); if (r-i+1>k){ QuickSort(a, i+1 , r, k); }else if (r-i+1==k){ res = a[i]; } else { QuickSort(a, l, i-1, k-(r-i+1)); } } } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; }