Method 1: First sort the array, then traverse the first k number, at this time the complexity of O (NLGN);
Method 2: Maintain a maximum heap with a capacity of K (Introduction to the algorithm, chapter 6th), and then start the traversal from the k+1 element, and compare the maximum element in the heap, if it is greater than the maximum element is ignored, if it is less than the maximum element, then the secondary element is fed into the heap, and the largest element of the heap is removed
Method 3: Use a fast selection algorithm with a complexity of O (n) ....... .....
/*** Created by Elvalad on 2014/12/8. * Input n integers, output the smallest k*/Importjava.util.ArrayList;Importjava.util.Arrays;ImportJava.util.Scanner; Public classMinkarray {/*use a binary tree to represent a heap*/ Private classHeap {PrivateNode Root; Private classNode {intkey; Node left; Node right; Node (intkey) { This. Key =key; } } /*build the largest heap and insert elements into the appropriate location of the heap*/ Public voidPutintkey) {Root=put (root, key); } PrivateNode put (node X,intkey) { if(x = =NULL) return NewNode (key); intCMP = key-X.key; if(CMP > 0) { intTMP =X.key; X.key=key; Key=tmp; X.right=put (x.right, key); } Else if(CMP < 0) {X.left=put (x.left, key); } returnx; } Public voidDeletemax () {root=Deletemax (root); } Privatenode Deletemax (node x) {if(x = =NULL) return NULL; if(X.left = =NULL) && (X.right! =NULL)) { intTMP =X.key; X.right.key=X.key; X.key=tmp; X.right=Deletemax (x.right); } Else if(X.right = =NULL) && (X.left! =NULL)) { intTMP =X.key; X.left.key=X.key; X.key=tmp; X.left=Deletemax (X.left); } Else if(X.left = =NULL) && (X.right = =NULL) ) {x=NULL; } Else { intCMP = X.left.key-X.right.key; if(CMP >= 0) { intTMP =X.key; X.key=X.left.key; X.left.key=tmp; X.left=Deletemax (X.left); } Else { intTMP =X.key; X.key=X.right.key; X.right.key=tmp; X.right=Deletemax (x.right); } } returnx; } Public voidprintheap (Node x) {if(x = =NULL) return; System.out.println (X.key); Printheap (X.left); Printheap (X.right); } } /*use a generic sorting algorithm, and then sequentially output the first k elements*/ Public int[] MinKArray1 (int[] A,intk) {Arrays.sort (a); for(inti = 0; I < K; i++) {System.out.println (a[i]); } returnArrays.copyofrange (A, 0, K); } /*O (n) algorithm for fast selection algorithm*/ Public int[] MinKArray2 (int[] A,intk) {returnA; } /*maintenance of a maximum heap with a capacity of K, introduction to the algorithm, the 6th chapter of the heap sequencing*/ Public int[] MinKArray3 (int[] A,intk) {Heap h=NewHeap (); for(inti = 0; I < K; i++) {h.put (a[i]); } for(inti = k; i < a.length; i++) { if(A[i] >=h.root.key) {Continue; } Else{h.put (a[i]); H.deletemax (); }} h.printheap (H.root); returnA; } Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); ArrayList<Integer> array =NewArraylist<integer>(); while(Scan.hasnext ()) {Array.add (Scan.nextint ()); } int[] A =New int[Array.size ()]; for(inti = 0; i < a.length; i++) {A[i]=Array.get (i); } Minkarray Mka=NewMinkarray (); Mka.minkarray1 (A,8); System.out.println ("---------------"); Mka.minkarray3 (A,8); }}
Find the smallest number of k in an array