Quicksort is one of the most efficient and commonly used sorting algorithms. It is a divide-and-conquer algorithm. It’s also an in-space algorithm as it does not use any extra space for sorting the input data, unlike the merge sort algorithm.
If you are a software engineer preparing for a tech interview, quicksort is one of the sorting algorithms you must be well-versed in. If you wish to learn quicksort in detail, check out our article on the Quicksort Algorithm. This post is dedicated to analyzing the “worst case” of quicksort. We’ll cover:
Before we get into the “when” and “why” of it, here are the best-case and worst-case scenarios of quicksort:

Quicksort works by dividing an array into smaller arrays and then sorting those smaller arrays. A pivot element is used to partition an array into smaller arrays; smaller arrays are divided recursively until an array with only one or no elements is created. Hence, the selection of the pivot element plays an important role in the efficiency of the quicksort algorithm.
But when the pivot element divides the array into two unbalanced sub-arrays (huge difference in size), the performance of quicksort decreases. Following are the cases where the pivot divides an array into two unbalanced sub-arrays:
Question 1: Can quicksort be implemented in O(NLogN) worst-case time complexity?
Answer: Yes, we can minimize the worst-case time complexity of quicksort to O(N*logN) by finding the median of the unsorted array in O(N) and using the median as the pivot. By doing this, we make sure that the array is divided into two subarrays of almost equal size, and we never encounter the case when the array is divided into subarrays of size 0 and N-1. But the constant factor of this method is very high, and therefore, it is not used in practice.
Question 2: Despite having a worst-case time complexity of O(N^2), why is quicksort considered a fast sorting algorithm?
Answer: The worst case of quicksort O(N^2) can be easily avoided with a high probability by choosing the right pivot. Obtaining an average-case behavior by choosing the right pivot element makes the performance better and as efficient as merge sort. Quicksort, in particular, exhibits good cache locality, which makes it faster than merge sort in many cases, such as in a virtual memory environment.
Head to Interview Kickstart’s learn page for more articles on sorting, such as:
Interview Kickstart offers the best technical interview prep courses that make you a better engineer and help you nail tech interviews.
As pioneers in the field of technical interview prep, we have trained thousands of software engineers to crack the toughest coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
For more information on what it takes to prepare for and succeed at FAANG tech interviews, sign up for our free webinar.
——–
Article contributed by Abhinav Tiwari
Time Zone:
100% Free — No credit card needed.
Time Zone:
Land high-paying DE jobs by enrolling in the most comprehensive DE Interview Prep Course taught by FAANG+ engineers.
Ace the toughest backend interviews with this focused & structured Backend Interview Prep course taught by FAANG+ engineers.
Elevate your engineering career with this interview prep program designed for software engineers with less than 3 years of experience.
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
Time Zone:
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
25,000+ Professionals Trained
₹23 LPA Average Hike 60% Average Hike
600+ MAANG+ Instructors
Webinar Slot Blocked
Register for our webinar
Learn about hiring processes, interview strategies. Find the best course for you.
ⓘ Used to send reminder for webinar
Time Zone: Asia/Kolkata
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
Explore your personalized path to AI/ML/Gen AI success
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary