I know that the time complexity of quick sort in the worst case is $\Theta(n^2)$ and in the average case is $\Theta(n \log n)$. Can it be $\Theta(n\sqrt n)$ for certain inputs?

  • 3
    $\begingroup$ In the average case it's $\Theta(n\log n)$. It can probably be $\Theta(n\sqrt{n})$ for certain inputs of a certain structure. I suggest looking up the best and worst cases for quicksort, and trying to combine them. $\endgroup$ – Yuval Filmus Feb 7 '18 at 13:08

Assume the array is sorted. If you always pick the element at n/2 as pivot then the time is O(n log n). If you always pick the first or last element it’s $O(n^2)$.

So which pivot gives you $O(n^{1.5})$?

  • $\begingroup$ The third quarter? $\endgroup$ – Ahmad Feb 8 '18 at 5:54
  • 1
    $\begingroup$ Can you give a formal proof? $\endgroup$ – Mr. Sigma. Feb 8 '18 at 12:37
  • $\begingroup$ This answer is very much incomplete. And misleading too, for any constant $c$, if you always pick the element at $cn$ as your pivot the complexity is $\Theta(n \log n)$, and never $\Theta(n\sqrt n)$. $\endgroup$ – orlp Sep 28 '19 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.