Why is quicksort used in practice?

halovi picture halovi · Apr 21, 2009 · Viewed 8.5k times · Source

Quicksort has a worst-case performance of O(n2), but is still used widely in practice anyway. Why is this?

Answer

vartec picture vartec · Apr 21, 2009

You shouldn't center only on worst case and only on time complexity. It's more about average than worst, and it's about time and space.

Quicksort:

  • has average time complexity of Θ(n log n);
  • can be implemented with space complexity of Θ(log n);

Also have in account that big O notation doesn't take in account any constants, but in practice it does make difference if the algorithm is few times faster. Θ(n log n) means, that algorithm executes in K n log(n), where K is constant. Quicksort is the comparison-sort algorithm with the lowest K.