简体   繁体   中英

Fast algorithm to find the n1 th smallest number to n2 th smallest number in an array

I have an array, I attempt to find the n1 th smallest number until the n2 th smallest number in the array and store them in another array of size n2-n1+1 . Here n2 > n1 and both are realtively small compared to the size of the array (for example, the array size is 10000, n1=5 , n2=20 ).

I can sort this array first, and then retrieve the n1 th number, n1+1 th number until the n2 th number from the sorted array. But since n1 and n2 are usually relatively small compared to the size of the array, it is not necessary to sort the array completely. The algorithm should be able to stop in middle once reaches n2 I think.

I wonder if there is any algorithm, maybe a modified version of certian sorting algorithm that is specifacally good (by good I mean fast) at this problem. You can either use Python code or pseudo code as an illustration, thanks!

Since, the N1 and N2 are really small compared to size of array letus say N. We can have an implementation in O(N2 * LogN) using min heap data structures.

Steps

  1. Construct a min heap. Complexity of this operation will be O(N)
  2. Loop for a range of 0 to N2: Get the root element and call heapify. Ignore first N1 elements and return rest of the elements. Complexity of this step is O(1)+O(logN) Hence, overall we have O(N2 * logN)

Instead of sorting, if your array size is not very big you can use simple lookup table (kind of sorting here). Firstly iterate over array and just store lookup[array[i]]=true; and then just iterate over lookup and do something like:

for(...){
if(lookup[j]){
    ith++; 
    if(ith>=n1 && ith<=n2)
       ADD(j);
}}

That would be O(n), if you have a window n1<=n2 rather nothing faster than O(n) exist

Use selection sort . It's O( n ²) if you sort the entire array, but O( mn ) if you only sort the smallest m items in the array.

If n2 (and therefore n1 ) are both small, then you can find the n2 smallest elements and ignore the first n1 ones. These approaches are described by Arun Kumar and user448810 and will be efficient as long as n1 remains small.

However, you may be describing a situation in which n1 (and therefore n2 ) can grow (perhaps even linearly with the overall list length) and it is only their difference n2-n1 which remains small. In this case you need a selection algorithm such as quickselect which will remain O(N) in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM