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$O(n^2)$$O(n \log n)$$\Theta(n\log n)$$O(n^3)$ Algorithms gatecse-2014-set3 algorithms sorting easy quick-sort + – go_editor 43.6k views answer comment Share Follow Print See all 11 Comments 11 11 Comments reply Show 8 previous comments ayush_27 commented Jan 19, 2024 reply Follow flag NOTE: Central or middle $\neq$ Median It may so happen that every time you choose the central element and that central element is either largest or smallest, this is the worst case scenario. Hence, $\Theta (n^2)$ time. Median = A number which has n/2 elements lesser than or equal to and n/2 elements are greater than or equal to it. If each time MEDIAN is chosen, then it is the best or avg case of quick sort, so in this case, time complexity will be $\Theta (nlogn)$ See below question: https://gateoverflow.in/1830/gate-cse-2006-question-52 10 10 replyShare Aditya_Khopade commented Oct 3, 2025 reply Follow flag I thought the same thing at the moment i saw the question. later saw your comment on median != central. Thanks for answer 2 2 replyShare Jayvijay Chauhan commented Apr 23 reply Follow flag The central element is simply midpoint of an array based on index position, whereas the median is the middle value of a sorted dataset that splits it into two equal halves 0 0 replyShare Please log in or register to add a comment.
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). Arjun answered Oct 8, 2014 • edited May 12, 2021 by soujanyareddy13 Arjun comment Share Follow See all 22 Comments 22 22 Comments reply Show 19 previous comments Kiyoshi commented Jun 8, 2021 reply Follow flag Arjun Sir’s comment, I’m not getting this line clearly. 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 is this means the other case in sorted array like if we take 7 in above sequence?? then also we get 1 and n-1 partitions of pivot. please anyone correct if I’m wrong... 0 0 replyShare pavansan commented Dec 15, 2025 reply Follow flag central element not means it is median even if at central element there may exist smallest or largest element so worst case time complexity dont change. 0 0 replyShare 0shan commented Jan 23 reply Follow flag What i can summarize is For every fixed position of pivot element in quick sort algo their exists an order of elements in array that would lead to worst behaviour of O(n^2). 2 2 replyShare Please log in or register to add a comment.
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). Shivam_m7 answered Jan 14, 2017 Shivam_m7 comment Share Follow See 1 comment 1 1 comment reply zgod commented Nov 10, 2025 reply Follow flag goat brooo 1 1 replyShare Please log in or register to add a comment.
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. Gate Keeda answered Oct 8, 2014 Gate Keeda comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments Brij Mohan Gupta commented Feb 24, 2016 reply Follow flag When All n Element are identical then complexcity will be O(n^2). 1 1 replyShare One commented Jul 9, 2016 i edited by One Jul 9, 2016 reply Follow flag if you take center or median as a pivot element then also worst case complexity will be O(n^2) because if you take all elements same then no effect of taking median it will take O(n^2) but you can say that all elements are distinct and you take the pivot as median then it will be O(nlog n) 3 3 replyShare siddharths067 commented Aug 5, 2020 reply Follow flag Please add a feature to remove answers with incorrect reasons, they do more harm than good. 2 2 replyShare Please log in or register to add a comment.
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. kaustabpal answered Sep 25, 2018 kaustabpal comment Share Follow 0 reply Please log in or register to add a comment.
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. Johnny1001 answered Jan 15, 2023 Johnny1001 comment Share Follow 0 reply Please log in or register to add a comment.
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. admin answered Sep 12, 2015 1 flag: ✌ Edit necessary (Jayanthc137 “wrong answer i think”) admin comment Share Follow See 1 comment 1 1 comment reply mrinmoyh commented Mar 20, 2019 reply Follow flag @admin when we choose central element as pivot then T.c will be O(nlogn) assumed that array is sorted in ascending or descending order but if the array is unsorted then every time in worst case that central element can go into extreme position (left or right) & cause T.C O(n^2). 0 0 replyShare Please log in or register to add a comment.