0
$\begingroup$

What would the worst case array look like if I decide to always take the element on the position $\frac{n}{2}$ as the pivot element? I know that if I choose the left or rightmost element as pivot ,the worst case occurs if:

  1. Array is already sorted in same order
  2. Array is already sorted in reverse order
  3. All elements are same

and that the complexity in that cases is $\mathcal{O}({n^2})$. However, this cases should not be a problem if I take the middle index of the partition as my pivot element.

asked May 7, 2019 at 20:28
$\endgroup$
1

1 Answer 1

3
$\begingroup$

Since you're not guaranteed anything about the order of the input, it's possible that the n/2 position has the smallest/largest element of the input. Then quicksort will proceed and put everything else on one side of the pivot.

answered May 7, 2019 at 20:50
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.