0

This is the case in limit N,100 SQL statements.

To sort the entire array is a waste when I just want to fetch the N~N+100 biggest elements.

What's the best algorithm for such situation?

1

3 Answers 3

2

Lookup an STL inplementation of std::partial_sort (C++)

Sign up to request clarification or add additional context in comments.

1 Comment

My bad, +1... so it's partial sort :)
2

The best you can hope for in an unsorted array is O(n) since all elements have to be checked. While traversing you simply keep the current top 100 elements in memory and if you encounter an element greater than the lowest element in the current 100, you simply remove that and add the new one. You can keep the top 100 elements in memory as a sorted queue.

3 Comments

Do you have a name for this algorithm ?
@new_perl: This is insertion sort, dropping anything else than the 100 biggest elements.
I don't have a name but I believe this is a case where the most obvious algorithm works ok. As Dennis pointed out, the technique is similar to insertion sort.
1

I guess that would depend on the size of the array.

For a sufficiently small array, as in selection sort, just iterate 100 times through the array, select the maximum element and remove it.

If the array is big, you could adapt the quicksort algorithm so that, in each step, it sorts either the elements bigger or smaller than the pivot, depending on if there are enough elements bigger than the pivot.

The quicksort algorithm could switch to the selection sort algorithm once the buckets become small enough.

4 Comments

How do you know the proper pivot ?
To avoid worst-case scenarios, pivots usually get selected either at random or as median of the first, last and middle element of the bucket.
Even for small arrays (I'm assuming here more than 100 elements), I don't see the point of traversing the array 100 times, when you can do it in 1 pass.
@LuchianGrigore: You are right. Your method is better than my method for small arrays. +1 for it. Still, large arrays should be narrowed down using adapted quicksort.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.