c++ - Search Algorithm to find the k lowest values in a list -


I have a list containing n double values ​​and I k The lowest list in the list

  • k n
  • is the initial list n Double values ​​are ordered randomly
  • k is not required to sort at least two values ​​

    Which algorithms would you recommend?

    At this time I use Quicksort to sort the entire list, and then for the first time I K element sorted list I hope that should be very fast algorithm.

    Thank you for your help!

    You can use to find the lowest element, and then repeat it and Return it back and all the elements which are less, this is If the list can be duplicated (to ensure that you do not need more elements, which you do not need), more work is done.
    This solution is o (n) the selection algorithm is implemented in C ++

    Another option maximum size K , and repeat the elements to maintain the pile to catch everyone, the minimum element.

      for each element x: if (heap.size () & lt; k): heap.add (x) and if x & lt; Heap.max (): heap.pop () heap.add (x)   

    When you do - the smallest elements in the heap are
    this solution o (nlogk )

Comments