Quick selection can be in the time complexity of the NLOGN to get a set of unordered number of the K large, based on a quick ordering, each time a number for the midpoint divided into the left and right two segments, the left is less than equal to the number of partitions, and the rights are greater than equal to the number of split, the fast choice of the solution faster than all sorts
/** * Created by Tcgogogo on 16/8/18. */class Solution {public void OutPut (int[] nums) {for (int i = 0; i < nums.length; i++) {System . Out.print (Nums[i] + ""); } System.out.println (); } public void Swap (int[] nums, int i, int j) {if (i = = j) {return; } int tmp = Nums[i]; Nums[i] = Nums[j]; NUMS[J] = tmp; } public void QuickSort (int[] nums, int. left, int. right) {if (left > right) {return; } int i = left and j = right; while (I < J) {while (I < J && Nums[j] >= Nums[left]) {J--; } while (I < J && Nums[i] <= Nums[left]) {i + +; } if (I < j) {Swap (Nums, I, J); }} swap (Nums, left, i); QuickSort (Nums, left, i-1); QuickSort (Nums, i + 1, right); } public int Quickselect (int[] nums, int left, int. right, int k) {if (left > right) {return 0; } int i = left and j = right; while (I < J) {while (I < J && Nums[j] >= Nums[left]) {J--; } while (I < J && Nums[i] <= Nums[left]) {i + +; } swap (Nums, I, J); } swap (Nums, left, i); if (k = = I-left + 1) {return nums[i]; } else if (K < I-left + 1) {return Quickselect (Nums, left, i-1, K); } else {return quickselect (nums, i + 1, right, K-(I-left + 1)); } }}
Quick Sorting and quick selection (Java)