logoalt Hacker News

strstrtoday at 2:06 AM1 replyview on HN

You can definitely do this without sorting.

QuickSelect is average case n, and is, roughly, quick sort where you throw away one of the sides each time and recurse on the other. This has a fat tail for cases where you pick a bad pivot (similar to quicksort), but you can median-of-medians your way out of that problem if someone cares. (Median of medians being where you subdivide the array into, say, 5 arrays, recursively compute the median on those, and pick the middle median as your pivot, which guarantees linear progress per iteration)


Replies

AdieuToLogictoday at 3:25 AM

> You can definitely do this without sorting.

> QuickSelect is ...

Quickselect implementations can, and often do, partially sort the underlying collection:

  As with quicksort, quickselect is generally implemented as 
  an in-place algorithm, and beyond selecting the kth 
  element, it also partially sorts the data.[0]
If you are aware of a quickselect implementation having O(n) average performance which does not modify the underlying collection, I would very much appreciate a reference to same.

0 - https://en.wikipedia.org/wiki/Quickselect