How can we find the ith largest element of an array?

Algorithm for finding the nth smallest / largest element in an array using a binary search tree with data balancing.

Read the post: Find the smallest kth element in the binary search tree in an Optimal way . But the correct answer is not clear, since I cannot understand the correct answer, for example, what I took ...... Please, some more explanations ........

+4
source share
7 answers

CAR Hoare select algorithm is designed specifically for this purpose. It runs in [expected] linear time with logarithmic additional storage.

Edit: An obvious alternative to sorting, then choosing the right item has O (N log N) complexity instead of O (N). Storing i largest elements in sorted order requires O (i) auxiliary storage and gross complexity O (N * i log i). It can be a gain if i is known a priori to be quite small (for example, 1 or 2). For a more general use, select usually better.

Edit2: not good, I don't have good help, but described the idea in a previous answer .

+13
source

First sort the array by lowering it, then take the ith element.

+2
source

Create an ordered data structure for storing i elements and set the initial counter to 0.

Process each element in the original array by adding it to this new structure until the new structure is filled.

Then process the rest of the original array. For each that is larger than the smallest in the sorted data structure, remove the smallest from this structure and add a new one.

After processing all the elements in the original array, your structure will contain the i largest elements. Just grab the last one and you have the biggest element.

Voila!

Alternatively, sort it and then just take the i'th element.

0
source

This is a suitable task for heaps that have a very low insertion and low delete_min cost. For instance. pairing heaps . It would have the worst result O (n * log (n)). But since it’s not trivial to implement, it’s better to check first the selection algorithms proposed elsewhere.

0
source

There are many strategies for your task (unless you start focusing on the self-balancing tree).

This is usually compilation speed / memory. Most algorithms require either an in-place array change or O(N) additional storage.

The self-balancing tree solution is in the latter category, but here it is not the right choice. The problem is that building the tree itself requires O(N*log N) , which will dominate the later search term and give the final complexity O(N*log N) . Therefore, you are no better than just sorting an array and using a complex data structure ...

In general, the problem largely depends on the value of i associated with N If you think for a minute, for i == 1 is this trivial right? He called the search for the maximum.

Well, the same strategy obviously works for i == 2 (carrying two max elements around) in linear time. And it is also trivially symmetrical: i.e. If you need to find the N-1st element, then just carry around two minimal elements.

However, it loses efficiency when i is about N / 2 or N / 4. Transferring the maximum elements i means sorting an array of size i ... and thus we retreat from the wall N*log N

Jerry Coffin pointed out a simple solution that is well suited for this case. Here is a link to Wikipedia . The full article also describes the median of the median method: it is more reliable, but requires more work and, as a rule, slower.

0
source
 Create an empty list L For each element x in the original list, add x in sorted position to L if L has more than i elements, pop the smallest one off L if List2 has i elements, return the i-th element, else return failure 

This should take O (N (log (i))). If I am considered to be a constant, then this is O (N).

0
source

Create a bunch of elements and call MIN i times.

0
source

Source: https://habr.com/ru/post/1313956/


All Articles