edited by
43,628 views
90 90 votes

You have an array of $n$ elements. Suppose you implement quicksort by always choosing the central element of the array as the pivot. Then the tightest upper bound for the worst case performance is

  1. $O(n^2)$
  2. $O(n \log n)$
  3. $\Theta(n\log n)$
  4. $O(n^3)$

10 Answers

Best answer
150 150 votes

Correct Option: A – $O(n^2)$.

When we choose the first element as the pivot, the worst case of quick sort comes if the input is sorted- either in ascending or descending order. Now, when we choose the middle element as pivot, sorted input no longer gives worst case behavior. But, there will be some permutation of the input numbers which will be giving the same worst case behavior. For example,

$1 \ 2 \ 3 \ 4 \ 5   \ 6  \ 7$
This array gives worst case behavior for quick sort when the first element is pivot.

$6 \ 4 \ 2 \ 1 \ 3 \ 5 \ 7$
This array gives the worst case behavior of $O(n^2) $ if we take middle element as the pivot- each split will be $1$ element on one side and $n-1$ elements on other side. Similarly, for any input, we can have a permutation where the behavior is like this. So, whichever element we take as pivot it gives worst case complexity of $O(n^2)$ as long as pivot is from a fixed position (not random position as in randomized quick sort).

edited by
23 23 votes

You already know that when choosing the first or last element as pivot, quick sort gives worst case complexity in ascending/descending order. It also gives worst case O(n2) when all elements are same.

Simply, check for all these cases first.

So, in ascending/descending order, choosing mid element as pivot gives O(nlogn) but choosing all elements same such as [2 2 2 2 2] still gives O(n2).

 

4 4 votes
A.

The Worst case time complexity of quick sort is O (n^2). This will happen when the elements
of the input array are already in order (ascending or descending), irrespective of position of
pivot element in array.
2 2 votes

I think the simplest way to solve this question is if we consider an array of n elements where all the elements are the same since it is not mentioned in the question that the elements need to be distinct. In this case Quick Sort will take $O(n^{2})$ time. Therefore the option will be A.

2 2 votes

The tightest upper bound for the worst case performance of quicksort when always choosing the central element of the array as the pivot is O(n^2).

This is because if the input array is already sorted or if the array is sorted in the reverse order and the pivot is always the central element, the partition will always result in one side having n-1 elements and the other side having 0 elements, leading to a worst-case scenario where each partition takes O(n) time and the overall quicksort takes O(n^2) time.

It's important to note that this is an worst-case scenario, and in practice, the average case performance of quicksort is O(n*log(n)) which makes it a good choice for sorting large data sets in most cases.

Also, this worst case is not so likely to happen if the pivot is chosen randomly, rather than always using the central element as pivot.

1 1 vote
when we select the central element as pivot element then the worst case time complexity will be O(nlogn).You may get doubt why not always choose mid element as pivot,this is because although this produceses O(nlogn) time complexity when we compute the constant time also then this will be more than selecting other elements as pivot.
1 flag:
✌ Edit necessary (Jayanthc137 “wrong answer i think”)
Answer:
Position:
Show:

Related questions

9 9 votes
5 answers 5 answers
8.8k
8.8k views
go_editor asked Sep 28, 2014
8,845 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
61 61 votes
9 answers 9 answers
29.1k
29.1k views
go_editor asked Sep 26, 2014
29,100 views
Let $P$ be quicksort program to sort numbers in ascending order using the first element as the pivot. Let $t_1$ and $t_2$ be the number of comparisons made by P for the i...
8 8 votes
6 6 answers
3.8k
3.8k views
Arjun asked Feb 27, 2025
3,796 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
59 59 votes
4 answers 4 answers
20.6k
20.6k views
Kathleen asked Sep 14, 2014
20,613 views
Randomized quicksort is an extension of quicksort where the pivot is chosen randomly. What is the worst case complexity of sorting n numbers using Randomized quicksort?$O...