Find the smallest number of K in an unordered array

Source: Internet
Author: User

Title: Given an unordered array of integers arr, find the smallest number of K

Requirements: If the length of the array arr is n, the minimum number of K is naturally obtained after sorting, when the complexity of time is the same as the time complexity of the sequencing is O (NLOGN), the subject requires time complexity of O (NLOGK).

1.O (Nlogk) method, that is, has maintained a K number of the largest large heap, the heap is currently selected K minimum number, in the heap of k elements in the heap top element is the largest one.

Next, iterate through the entire array, and see if the current number is smaller than the top element of the heap. If so, replace the elements of the heap top with the current number, then adjust the heap from the top of the heap, and the position of the largest element of the heap is still at the top of the heap, and if it is not, proceed to the next number without any action, and the number of k in the heap is the smallest number of k in all arrays.

See the Getminknumsbyheap method in the code below, and the Heapinsert and Heapify methods in the code are the implementation of the heap and the tuning heap, respectively, in the heap ordering.

1  Public int[] Getminknumsbyheap (int[] arr,intk)2 {3     if(K<1 | | k>arr.length)4             returnarr;5     int[] kheap=New int[K];6      for(inti=0;i<k;i++)7     {8 Heapinsert (kheap,arr[i],i);9      }Ten     for(inti=k;i<arr.length;i++) One   { A        if(arr[i]<kheap[0]) -      { -kheap[0]=Arr[i]; theHeapify (kheap,0, k); -      } -    } -   returnkheap; + } -  +  Public voidHeapinsert (int[] arr,intValueintindex) A { atarr[index]=value; -     while(index!=0) -   { -     intParent= (index-1)/2; -     if(arr[parent]<Arr[index]) -     { in swap (arr,parent,index); -index=parent; to     } +   Else -       Break; the   } * } $ Panax Notoginseng  Public voidHeapify (int[] arr,intIndexintheapsize) - { the     intLeft=index*2+1; +     intRight=index*2+2; A     intlargest=index; the     while(left<heapsize) +  { -      if(arr[left]>Arr[index]) $largest=Left ; $      if(rightArr[largest]) -largest=Right ; -      if(largest!=index) the swap (arr,largest,index); -       ElseWuyi          Break; theindex=largest; -Left=index*2+1; WuRight=index*2+2; -     } About } $  -  Public voidSwapint[] arr,intIndex1,intindex2) - { -     inttmp=Arr[index1]; Aarr[index1]=Arr[index2]; +arr[index2]=tmp; the}
View Code

Find the smallest number of K in an unordered array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.