Thought, the use of fast-line thinking, constantly looking for decomposition points, the demarcation point of the subscript and K-1 comparison if equal, return the value, otherwise update the left and right boundary. When the value in the left and right bounds is less than or equal to 2, the insertion sort is used to return a[k-1]
int Findcut (vector<int>& A, int l, int r) {if (L + 1 >= r) {return-1;} int pivot = A[l];int i = l, j = R;while (true) {do{i++;} while (I<r&&a[i] < pivot);d o{j--;} while (J>l& ; &a[j] >= pivot); if (i >= j) Break;int temp = a[i];a[i] = a[j];a[j] = temp;} A[L] = A[j];a[j] = Pivot;return J;} void Insert_sort (vector<int>&a, int l, int r) {for (int i = l + 1; i < R; i++) {int j=i,pivot = A[i];while (J&G T;l&&pivot < a[j-1]) {a[j] = a[j-1];j--;} A[J] = pivot;}} int findkth (vector<int>a, int n, int K) {int left = 0, right = N;while (Left+1 < right) {int cut = Findcut (A, left, right), if (cut = = K-1) return A[cut];else if (Cut < K-1) {left = cut + 1;} Else{right = Cut;}} Insert_sort (A, left, right); return a[k-1];}
Look for the number of the first K small.