Article written by Shashi Kadapa, under the guidance of Neeraj Jhawar, a Senior Software Development Manager and Engineering Leader. Reviewed by Mrudang Vora, an Engineering Leader with 15+ years of experience.
Quick sort is used in many systems, such as Java’s Arrays.sort() and Python’s built-in sorting methods. It carries out operations in O(n log n) time, and is efficient in processing large datasets.
In interviews, you are evaluated on quick sort algorithm, and implementation in applications. This blog presents several important concepts and answers to commonly asked questions in quick sort interviews.
Quick sort is a sorting algorithm that sorts data by choosing a pivot element and splitting the array into two parts: values smaller than the pivot and values larger than it. The same process is then repeated recursively on both sections until the entire array is ordered.
It is widely used because it works in-place, is highly cache-friendly, and delivers strong average-case performance.
When the pivot selection has uneven partitions, the performance reduces. Examples are sorted and improperly arranged input data.
The steps for quick sort are:
Pass-by-pass Dry Run of Quick Sort: The dry run of quick sort is given for example array: [8, 3, 1, 7, 0, 10, 2]. The last element is the pivot.
| Pass | Pivot | Array After Partition |
|---|---|---|
| Initial | — | [8, 3, 1, 7, 0, 10, 2] |
| Pass 1 | 2 | [1, 0, 2, 7, 3, 10, 8] |
| Pass 2 (Left Side) | 0 | [0, 1, 2, 7, 3, 10, 8] |
| Pass 3 (Right Side) | 8 | [0, 1, 2, 7, 3, 8, 10] |
| Pass 4 (Subarray) | 3 | [0, 1, 2, 3, 7, 8, 10] |
Key partition invariant rule:
While partitioning, quick sort maintains an important condition:
This rule allows quick sort to break the problem into smaller, independent parts, making the algorithm fast and efficient for large datasets.
Quick Sort Dry Run Table
Array: [8, 3, 1, 7, 0, 10, 2]. Pivot = last element (2).
| Step | Array State | Action |
| Initial | [8, 3, 1, 7, 0, 10, 2] | Pivot selected: 2 (last element) |
| After partition | [0, 1, 2, 7, 3, 10, 8] | Pivot (2) in final position. Left: [0,1]. Right: [7,3,10,8] |
| Recurse left | [0, 1] | Already sorted – base case |
| Recurse right | [3, 7, 8, 10] | Recursively sorted |
| Final | [0, 1, 2, 3, 7, 8, 10] | Sort complete |
Pivot breaks up the array into parts. A pivot can be picked from the beginning, end, middle, or even randomly from the array. Smarter choices, such as a random pivot or median-of-three, often lead to better balance and faster sorting. The following table shows some of the quic
| Strategy | Process of working | Advantages | It fails when … |
| First element | Always use arr[0] as pivot | Simple to implement | Worst case O(n squared) on sorted or reverse-sorted arrays |
| Last element | Always use arr[n-1] as pivot | Simple to implement | Same worst case as first element on sorted input |
| Random element | Choose a random index as pivot | Avoids worst case in practice | Requires random number generation |
| Median of three | Use median of first, middle, last elements | Best practical performance | Slightly more complex to implement |
The steps are:
Quick Sort is a divide-and-conquer sorting algorithm that works by selecting a pivot element and arranging the array around it. The partition function places smaller elements on one side of the pivot and larger elements on the other. The quickSort function then recursively sorts the left and right subarrays.
Algorithm quickSort(arr, low, high) If low < high pivotIndex = partition(arr, low, high) quickSort(arr, low, pivotIndex - 1) quickSort(arr, pivotIndex + 1, high) Algorithm partition(arr, low, high) pivot = arr[high] i = low - 1 For j = low to high - 1 If arr[j] < pivot i = i + 1 Swap arr[i] and arr[j] Swap arr[i + 1] and arr[high] Return i + 1
Python offers a clean and readable way to implement Quick Sort. The algorithm works by selecting a pivot element, dividing the array into smaller sections, and recursively sorting each part.
The example below includes both the quickSort() function and the partition() helper function.
def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] < pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 def quickSort(arr, low, high): if low < high: pivotIndex = partition(arr, low, high) quickSort(arr, low, pivotIndex - 1) quickSort(arr, pivotIndex + 1, high) # Driver code arr = [8, 3, 1, 7, 0, 10, 2] quickSort(arr, 0, len(arr) - 1) print("Sorted Array:", arr)
Java uses methods and classes to organize the Quick Sort algorithm. The partition() method places the pivot in its correct position, while the quickSort() method recursively sorts the remaining elements.
class QuickSort { int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } void printArray(int arr[]) { for (int num : arr) { System.out.print(num + " "); } } public static void main(String[] args) { int arr[] = {8, 3, 1, 7, 0, 10, 2}; QuickSort obj = new QuickSort(); obj.quickSort(arr, 0, arr.length - 1); System.out.println("Sorted Array:"); obj.printArray(arr); } }
C++ is commonly used for implementing efficient sorting algorithms because of its speed and memory control. The following example shows a complete Quick Sort implementation with separate partition and recursive sorting functions.
#include <iostream> using namespace std; int partition(int arr[], int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[high]); return i + 1; } void quickSort(int arr[], int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { cout << arr[i] << " "; } } int main() { int arr[] = {8, 3, 1, 7, 0, 10, 2}; int size = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, size - 1); cout << "Sorted Array: "; printArray(arr, size); return 0; }
Quick Sort gives fast performance with large datasets and is implemented used in many applications. Its efficiency depends on how evenly the array is divided during each partition step. In most situations, Quick Sort performs very well, but poor pivot selection can lead to slower execution. The following is a quick sort complexity table:
| Case | Time Complexity | When It Occurs | Why |
| Best case | O(n log n) | Pivot always lands near the middle | Recursion tree is balanced – log n levels, each O(n) work |
| Average case | O(n log n) | Random or well-distributed input | Balanced enough on average – same structure as best case |
| Worst case | O(n squared) | Pivot always min or max remaining element | Recursion tree becomes linear – n levels, each O(n) work |
| Space | O(log n) | Average case call stack depth | In-place – no auxiliary array, but recursion uses stack space |
Quick Sort is generally treated as an in-place sorting algorithm because it sorts the elements within the original array itself. It does not need an extra array for processing, and the additional memory usage mainly comes from recursive function calls. On average, the recursion stack requires about O(log n) space.
The standard implementation of Quick Sort is not stable. While partitioning the array, elements with the same value can be swapped into different positions, changing their original order. Because of this behavior, equal elements may not remain in the same sequence after sorting.
Although stable versions of Quick Sort do exist, they usually require extra memory or more complex partitioning techniques. This makes the algorithm harder to implement compared to the traditional version. It is also a common interview question to ask whether Quick Sort is stable and in-place, especially when comparing it with algorithms such as Merge Sort.
Two popular partitioning techniques in Quick Sort are Lomuto and Hoare. Although they both split the array around a pivot element, their methods for scanning and exchanging elements differ. Performance, swap count, and efficiency on various input data types can all be impacted by the partition scheme selection. The following table compares Lomuto and Hoare partition schemes.
| Aspect | Lomuto Partition | Hoare’s Partition |
| How it works | One pointer from left, swaps smaller elements toward front | Two pointers from both ends, swap when out of order |
| Pivot placement | Pivot ends up in its final position after partition | Pivot does not necessarily end up in final position |
| Number of swaps | More swaps on average | Fewer swaps on average – more efficient |
| Code complexity | Simpler to understand and implement | Slightly more complex |
| Common usage | Teaching and beginner implementations | Production implementations |
| Worst case input | Sorted array with last element as pivot | Sorted array with first element as pivot |
The method of the Lomuto partition scheme places smaller elements ahead of the pivot by scanning the array from left to right. The pivot is switched to its proper position at the conclusion. It is frequently used in instructional examples and coding interviews because the concept is simple.
Two pointers that approach one another from both sides of the array are used in the Hoare partition scheme. Elements are instantly switched if they are discovered on the incorrect side.
Quick Sort and Merge Sort are two of the most widely used sorting algorithms. Both follow the divide-and-conquer approach, where the problem is broken into smaller parts and solved recursively. Even though their average performance is similar, they differ in memory usage, stability, and behavior in worst-case situations. The following table provides a comparision between quick & merge sort.
| Aspect | Quick Sort | Merge Sort |
| Average time | O(n log n) | O(n log n) |
| Worst case | O(n squared) – bad pivot | O(n log n) – always |
| Space | O(log n) – call stack only | O(n) – auxiliary array |
| Stable? | No | Yes |
| Cache performance | Better – in-place sequential access | Worse – auxiliary array causes cache misses |
| Best for | General in-memory sorting of large arrays | Linked lists, external sort, when stability required |
Also Read: Time Complexities of All Sorting Algorithms
Quick sort frequently appears in technical interviews because it combines sorting, recursion, and partitioning concepts in one algorithm. Interviewers use quick sort–based problems to check how candidates handle arrays, optimize performance, and think through edge cases.
Questions such as finding the kth largest element or rearranging numbers around a pivot are commonly inspired by quick sort. Even when the algorithm itself is not asked directly, its core ideas are often part of problem-solving discussions.
| Pattern | How Quicksort Applies | Example Problems |
|---|---|---|
| Sort in place | Standard quicksort – partition and recurse without auxiliary array | LeetCode 912: Sort an Array |
| QuickSelect (Kth element) | Partition step finds pivot’s final position – only recurse on the side containing K | LeetCode 215: Kth Largest Element in Array |
| Dutch National Flag | Three-way partition for arrays with 3 distinct values (0s, 1s, 2s) | LeetCode 75: Sort Colors |
| Partial sort | Use QuickSelect to find top-K elements without fully sorting | Top K frequent elements pattern |
This blog examined several important topics in quick sort. Quick sort algorithms and how quick sort works were examined with examples. Pivot selection strategies in quick sort, pseudocode, and quick sort programs in Java, Python, and C# were discussed.
Quick sort offers O(n log n) average performance, works in-place, and is widely used for sorting large arrays in memory. Understanding concepts like worst-case behavior, pivot selection, and QuickSelect helps build deeper algorithmic knowledge instead of simple memorization. These topics are also highly important for strong FAANG Interview Preparation and technical coding interview success.
Quick sort is often faster because it sorts within the same array, improving cache efficiency and reducing extra memory usage compared to merge sort.
Yes, many real-world systems use optimized versions of quick sort because of its speed and efficiency on large datasets.
Quicksort sorts the entire array, while QuickSelect finds only the required kth element, making it faster for selection problems.
Time Zone:
100% Free — No credit card needed.
Time Zone:
Master ML interviews with DSA, ML System Design, Supervised/Unsupervised Learning, DL, and FAANG-level interview prep.
Get strategies to ace TPM interviews with training in program planning, execution, reporting, and behavioral frameworks.
Course covering SQL, ETL pipelines, data modeling, scalable systems, and FAANG interview prep to land top DE roles.
Course covering Embedded C, microcontrollers, system design, and debugging to crack FAANG-level Embedded SWE interviews.
Nail FAANG+ Engineering Management interviews with focused training for leadership, Scalable System Design, and coding.
End-to-end prep program to master FAANG-level SQL, statistics, ML, A/B testing, DL, and FAANG-level DS interviews.
Learn to build AI agents to automate your repetitive workflows
Upskill yourself with AI and Machine learning skills
Prepare for the toughest interviews with FAANG+ mentorship
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
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Join 25,000+ tech professionals who’ve accelerated their careers with cutting-edge AI skills
Webinar Slot Blocked
Time Zone: Asia/Kolkata
Hands-on AI/ML learning + interview prep to help you win
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
See you there!