Finding the number of K in an array in the algorithm problem

Source: Internet
Author: User
Tags array length array sort

Problem: Find the maximum number of K before an array.

Solution one (direct solution):

The array is sorted quickly, and then the number of k large is directly selected. The time complexity of this method is O (Nlog (N)). N is the original array length.

This solution contains a lot of redundancy because the entire array is sorted, and we don't actually need to do that.

Solution two (k array sort):

First, create an empty array of length K. Select the number of K from the original array to sort and place it in the empty array. The time complexity of this step is O (klog (K)).

Next, from the remaining n-k values, loop through and then compare to the number at the end of the array above (that is, the maximum number inside) and insert to the appropriate position. The time complexity of this step is O (n-k).

The total average time complexity is O (klog (K) + (n-k)).

Solution three (recursive method):

When the K value is very large or very close to N, the time complexity of the two solutions above will be significantly increased, which requires a better approach to deal with.

Suppose n=100, K=50 's case.

Select one of the median elements (whether the value is reasonable will affect the efficiency of the algorithm, the reason for the same fast sort), and then put the elements greater than or equal to the right side, less than the number of places to the left. For example, 7 4 6 8 0-1, select 6 as the median element, the result should be 4 0-1 6 8 7, then compare the K value and the current Median element 6 index (3).
If K is less than index 3, the element to the right, including the median element, is the top (3 (Index)-K + 1) Maximum number in the top K-largest number, which is saved and the recursive operation between index 0 ~ 2 continues to select the K name.
If k is greater than index 3, select K-3 (Index)-1 names recursively in 4 to 5.
Another key point is that you can select a critical point for the length of the array in the recursion, which is less than the critical, and is no longer recursive.

The average time complexity is O (N).

When the size of the problem is not very large, such as the array size n is very small, n is 100 order of magnitude, you can not pursue the efficiency of the algorithm, because the problem is small, the above three algorithms run time difference is not large,
It is entirely possible to consider the first or second relatively simple implementation.

Finding the number of K in an array in the algorithm problem

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.