In-depth explanation of the k-th big number problem and algorithm Overview

Source: Internet
Author: User

Solution 1:We can sort this out-of-order array in descending order, and then retrieve the first K, with the total time complexity of O (N * logn + k ).

Solution 2:Select sorting or interactive sorting to obtain the maximum K number after K selections. The total time complexity is O (n * K)

Solution 3: using the fast sorting idea, we randomly find an element x from array S and divide the array into two parts: SA and Sb. The element in SA is greater than or equal to X, and the element in Sb is less than X. There are two situations:
1. If the number of elements in SA is smaller than K, the K-| sa | element in Sb is the k-th number;
2. If the number of elements in SA is greater than or equal to K, the maximum K number in SA is returned. The time complexity is approximately O (n)

Solution 4:Binary [Smin, Smax] lookup result X, counts X in the array, and the number of larger than X in the array is the number of K-1, that is, the K number. The average time complexity is O (n * logn)

Solution 5:Use the O (4 * n) method to set up the maximum heap of the original number, and then pop it out K times. Time Complexity: O (4 * n + K * logn)

Solution 6:Maintain a minimum heap of K size. For each element in the array judgment and heap top size, if the heap top is large, no matter. Otherwise, the heap top is displayed, insert the current value to the heap. Time complexity O (N * logk)

Solution 7:Using hash to save the number of times the element Si appears in the array, using the counting sorting idea, in the process of linear scanning from large to small, the first number of k-1 is the K number, 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.