Depth of the K-large number of questions and the outline of the algorithm _c language

Source: Internet
Author: User

Solution 1: We can sort the random ordinal group in order from big to small, then remove the front k large, the total time complexity is O (N*logn + k).

Solution 2: use the choice of sorting or interactive sorting, K-time selection can get the number k large. Total time complexity O (n*k)

Solution 3: Using the idea of fast ordering, we randomly find an element x from the array s and divide the array into two parts SA and SB. The element in the SA is greater than or equal to the element less than x in X,SB. There are two kinds of situations:
1. The number of elements in the SA is less than K, then the first k-| in SB The sa| element is the number k large;
2. The number of elements in the SA greater than or equal to K returns the number of K in the SA. Time complexity is approximately o (n)

Solution 4: binary [Smin,smax] Find the result x, the statistic x appears in the array, and the number that is larger than X in the whole array is k-1 K. Time complexity average is O (N*LOGN)

Solution 5: use O (4*n) method to build the largest heap of the original number, and then pop out K-time. Time Complexity of O (4*n + k*logn)

Solution 6: maintain a k size of the smallest heap, for each element in the array to determine the size of the top of the heap, if the top of the heap is larger, regardless, otherwise, pop-up heap top, the current value into the heap. Time complexity O (n * logk)

Solution 7: using the hash to save the number of elements si in the array, using the idea of counting, linear from large to small scanning process, the number of k-1 in front of the number is K large, the average time complexity O (n)

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.