c++calgorithmsearch

Search Algorithm to find the k lowest values in a list (selection algorithm/problem)


I have a list that contains n double values and I need to find the k lowest double values in that list

What algorithm would you recommend?

At the moment I use Quicksort to sort the whole list, and then I take the first k elements out of the sorted list. I expect there should be a much faster algorithm.

Thank you for your help!!!


Solution

  • You can use selection algorithm to find the kth lowest element and then iterate and return it and all elements that are lower then it. More work has to be done if the list can contain duplicates (making sure you don't end up with more elements that you need).
    This solution is O(n). Selection algorithm is implemented in C++ as nth_element()

    Another alternative is to use a max heap of size k, and iterate the elements while maintaining the heap to hold all k smallest elements.

    for each element x:
       if (heap.size() < k):
          heap.add(x)
       else if x < heap.max():
          heap.pop()
          heap.add(x)
    

    When you are done - the heap contains k smallest elements.
    This solution is O(nlogk)