Problem Description: Ask for the maximum number of K for an array, for example, the maximum three numbers of {1,5,8,9,11,2,3} should be, 8,9,11
Problem Analysis:
1. Solution One: The most intuitive approach is to sort the array from large to small, and then select the largest number of K, but such a solution, the complexity is O (logn*n), but sometimes do not need to sort, with a simple choice of sorting, or bubble sort, then the K-wheel exchange or choice, you can draw a conclusion, The complexity is O (n*k), when the K is very large when the sorting may be a better solution, when the K is small when the choice or bubble efficiency will be more high. But this is going to sort the first k number, so the efficiency is not high, when K is very large, the above two methods are not very high efficiency.
2. Solution Two: Do not order the number of the first K, recall the algorithm of the fast, the partition function, is to randomly select a number in the array, put a number larger than this number, placed in front of the array, the number of smaller than this is placed in the array
Later, if you want to find the random number, the final position is K, then the largest number of K to find out, along this way to think about the problem, but this function, the final index position is not necessarily k, may be larger than K or k smaller, we will find the array into two parts sa,sb,sa is a large part, SB is a small part, if the length of the SA is equal to K, then the direct return is the final result, if the length of the SA is larger than K, then the SA is a new array, from the SA to find the largest number of K, this time to reduce the original data set to the SA, if the length of the SA is smaller than K, The addition of M elements in SA, then M elements counted as part of the element K, and then found from SB, k-m the largest element, combined is the final result, then the problem is reduced to find k-m from SB the largest element, so overall this is a recursive process, although the complex is also O (n *LOGN) However, each time the amount of data will be reduced so it will be much faster.
3. Solution Three: The use of heap sequencing, the establishment of a K-scale maximum heap, and then the data inserted into the team, then the time complexity of the insertion team is O (LOGK), suitable for large data volume, the effect of the heap is better.
Here we give the code of the solution two for your reference:
public class Main {private static void swap (int[] nums,int index1,int index2) {int temp=nums[index1];nums[index1]=nums[ Index2];nums[index2]=temp;} public static int partition (int[] nums,int start,int end) {int index=new Random (). Nextint (nums.length); int num=nums[ Index];swap (nums, index, 0), int i=start;int j=end;while (i<j) {while (i<j&&nums[j]<=num) {j--;} if (i<j) {nums[i++]=nums[j];} while (I<j&&nums[i]>num) {i++;} if (i<j) {nums[j--]=nums[i];}} Nums[i]=num;return i;} public static int partition (int[] nums) {return partition (Nums, 0, nums.length-1);} public static int[] Findkmax (int[] nums,int k,int start,int end) {int index=partition (nums,start,end); int length= Index-start+1;int[] tempmax=arrays.copyof (nums, length); int[] Tempmin=new int[nums.length-length]; System.arraycopy (Nums, index+1, tempmin, 0, nums.length-length); if (length>k) {return Findkmax (tempmax,k,0, TEMPMAX.LENGTH-1);} else if (length==k) {return tempmax;} Else{int[] Temp2=new int[k]; System.arraycopy (Tempmax, 0, Temp2, 0, tempmax.length); int[] Temp3=findkmax (tempmin,k-length,0,tempmin.length-1); System.arraycopy (temp3,0, Temp2, index+1, temp3.length); return temp2;}} public static int[] Findkmax (int[] nums,int k) {return Findkmax (nums, K, 0, nums.length-1);} public static void print (int[] nums) {for (int i=0;i<nums.length;i++) {System.out.print (nums[i]+ "");}} public static void Main (string[] args) {int[] nums={1,5,8,9,11,2,3};int[] Temp=findkmax (Nums, 3); System.out.println ("ret:");p rint (temp);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The maximum number of k in an array (Java)