Find the elements of the K-large in the array, various solutions and analysis

Source: Internet
Author: User

Encountered a very simple and interesting problem, you can see the different algorithm strategy to solve the problem of the optimization process.
Problem: Look for the element in the array of the K-large.

The simplest idea is to sort directly, and the algorithm complexity is O (N*LOGN). This is obviously inefficient, because no other information is required as long as the K-large element is computed. Of course, if you need frequent access to the K-large element in some cases, you can first sort the results directly.

The first way is to sort the first k elements with a sort of selection, bubbling, or swapping sort. These three algorithms may not be the fastest sorting algorithms. But there is a nature: the algorithm complexity of calculating the maximum (small) element is O (N). The process cannot be interrupted, and the third-largest element must be built on the basis of the second-largest element (because the current array is the largest one is computed each time). So its algorithm complexity is O (n*k);

The second method is to use the idea of quick sorting. Quick sort each time an element is swapped to the right position, and the left side is large and the right side is small. This algorithm selects a pivot element each time, after sorting, to see the location of the pivot element. If its position is greater than k, it indicates that the element of the first sub-sequence of the K-large is required. Conversely, if it is less than k, it is required to indicate the length of the previous sequence in the next sequence of K-elements.

In this way, the problem has been changed into a problem that can be solved with the quick-line thinking. For fast sorting, the algorithm complexity is O (N*LOGN). And the algorithm complexity of the algorithm is O (N). Why is it?

In fact, this place algorithm complexity analysis is very interesting. The first exchange, the algorithm complexity O (N), the next process and the rapid sorting is different, the fast sorting is to continue to process the data on both sides, and then merge, the algorithm complexity of the merging operation is O (1), so the total algorithm complexity is O (N*LOGN) (can be understood, each exchange with N, altogether logn times). However, there is no need to deal with the remaining half after determining the relative position of the pivot element (on the left or right side of K). In other words, the algorithm complexity of the second insertion is no longer o (N) but O (N/2), is it not the same? Actually, it's not the same, because the next process is 1+1/2+1/4+ ... < 2, in other words, the algorithm complexity of O (2N) is the algorithm complexity of O (N).

This algorithm I have seen in the data structure and algorithm book and the sword Point offer. It's a classic, classic algorithm. The reason for this is that he has worked hard to reduce the complexity of the algorithm by a bit in each recursion, and ultimately the complexity of the whole algorithm has dropped a lot, which is a very smart approach.

The third method is very simple, but using it requires a condition, that is, the input array has a small range of values, the best case is to be able to form a fully distributed, that is, 1000 size of the array inside the number is from 1 to 1000 such a child. First, create an array that can completely load the original array, where the exponential group size equals the largest element of the original array (and perhaps the optimization, but this is a little simpler), such as the original array is [1,2,3,4,5], the size of the array I want to generate is 5, if the original array is [5,3,6,10], The size of the array I want to generate is 10. Next, the original array is traversed, each element is placed at the corresponding subscript of the second array, and 5 is placed in the place labeled 5 (the actual process is reduced by 1, since the array starts with 0). Added element values to count the number of occurrences of this element. The algorithm complexity of this process is O (N). Next, iterate through the resulting array, and find the element K large.
What is the algorithm complexity of this process? In fact, this is related to the original array, the more discrete the original array is worse. For example, the original array is [1,1000], which is very bad. The second part of the algorithm complexity is O (m), M is the maximum value of the former array. The total algorithm complexity O (N) +o (M);

This shows that the third method of dealing with this problem is very bad. Although the third method is very restrictive (floating-point and negative numbers are also required for the size of the original array), the essence of the third method is a hash. is to turn the original mapping relationship into a reflection shot. That is, if a direct mapping of the data to the address is formed. But the problem of this mapping is also obvious, it can only be considered to pick up a blunder, if the input array a little side, or the same hash algorithm to calculate its hash value. The hash value is then mapped to the address.

The fourth method is to do it with a binary heap. The algorithm for constructing a binary heap on an array of size n is O (n). Then each time the algorithm complexity of the filter is O (Logn), altogether under the filter K times, the algorithm complexity is O (N+K*LOGN).

Obviously this is slower than the second method.

Find the elements of the K-large in the array, various solutions and analysis

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.