Tony Hoare came up with DSA Quicksort in 1959. It is a "Divide and Conquer" algorithm that has worked for a long time. Algorithms like Bubble Sort are good for teaching logic, while Quicksort is made to work fast.
DSA Quicksort Overview
Quicksort is a very fast way to sort things that uses a recursive method. It operates by choosing a "pivot" element from the array and splitting the other elements into two sub-arrays based on whether they are smaller or larger than the pivot.
- Divide: Choose a pivot element and split the array so that elements smaller than the pivot go to the left and elements larger than the pivot go to the right.
- Conquer: Do the same thing again and again to the left and right sub-arrays.
- Combine: There is no need for a separate "merging" phase because the sub-arrays are already sorted.
How Does Quicksort Work?
The Partitioning stage is what makes Quicksort work. Depending on how it is set up, the "pivot" can be different:
- Pick the first element as the pivot.
- Pick the last element as the pivot.
- Pick a random element as the pivot (Recommended for avoiding worst-case).
- Pick the median element as the pivot.
Step-by-Step Visualization of Quicksort
Let’s sort the array: [10, 80, 30, 90, 40, 50, 70] using the last element (70) as the pivot.
- Initial Partitioning:
- Compare 10 with 70. 10 < 70 (Keep it left).
- Compare 80 with 70. 80 > 70 (Move it right).
- Compare 30 with 70. 30 < 70 (Keep it left).
- Compare 90 with 70. 90 > 70 (Move it right).
- Compare 40 with 70. 40 < 70 (Keep it left).
- Compare 50 with 70. 50 < 70 (Keep it left).
- After First Pass: The array looks like [10, 30, 40, 50, 70, 90, 80].
- The pivot 70 is now in its correctly sorted position.
- Recursion: Now, apply Quicksort to the left sub-array [10, 30, 40, 50] and the right sub-array [90, 80].
- Completion: The process continues until sub-arrays have only one or zero elements.
Quicksort in Python
Python’s clean syntax makes it easy to visualize the recursive nature of DSA Quicksort.
Python
def quicksort(arr):
if len(arr) <= 1:
return arr
else:
# Picking the middle element as pivot
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
# Usage
data = [33, 10, 55, 71, 29, 1, 99, 5]
sorted_data = quicksort(data)
print(f"Sorted Array: {sorted_data}")
Note: While the above "list comprehension" method is easy to read, professional implementations often use "In-place Partitioning" to save memory.
Also Read :
DSA Quicksort At a Glance
Quicksort is known for its incredible speed, but it has a "weakness" depending on the pivot choice.
|
Case
|
Time Complexity |
When does it happen? |
|
Best Case
|
O(n log n) |
The pivot always divides the array into two equal halves.
|
| Average Case |
O(n log n) |
This is the standard performance in 99% of real-world scenarios. |
|
Worst Case
|
O(n²)
|
Happens when the pivot is always the smallest or largest element (e.g., sorting a already sorted array). |
| Space Complexity |
O(log n) |
Due to the recursive stack. It is much more memory-efficient than Merge Sort.
|
Comparison: Quicksort and Merge Sort
Why do developers often prefer Quicksort over Merge Sort? Here are the few differences:
|
Feature
|
Quicksort |
Merge Sort |
|
Strategy
|
Divide and Conquer |
Divide and Conquer |
| Extra Space |
Very little (O(log n)) |
Significant (O(n))
|
|
Stability
|
Not Stable |
Stable |
| Best For |
Arrays / Memory-limited systems |
Linked Lists / External Sorting
|
| Cache Friendly |
High (Contiguous access) |
Low
|
DSA Quicksort Applications
Here the real life applications of DSA Quicksort:
- Commercial Databases: Most SQL databases use Quicksort (or a variant called Introsort) to handle ORDER BY queries.
- Operating Systems: The qsort() method in C and the sort() utilities in many libraries are better variants of Quicksort.
- Big Data Analytics: Quicksort is used to sort unstructured logs in the early phases of data cleansing so that they can be processed faster.
Game Development: Quicksort is typically used to sort items by how far away they are from the camera (Z-sorting) because it is fast and doesn't take up much memory.