Introduction
Quick Sort is one of the most powerful and widely used sorting algorithms in computer science. It follows the Divide and Conquer approach and is known for its excellent average-case performance.
What makes Quick Sort special is:
- It sorts in-place (no extra array required)
- It is faster in practice than many O(n log n) algorithms like Merge Sort
- It is heavily used in real-world systems and libraries
In this article, weβll go deep into:
- Intuition behind Quick Sort
- Partition logic (most important part)
- Step-by-step dry run
- Java implementation with comments
- Time complexity analysis
- Common mistakes and optimizations
π Problem Link
Problem Statement
Given an array arr[], sort it in ascending order using Quick Sort.
Requirements:
- Use Divide and Conquer
- Choose pivot element
- Place pivot in correct position
- Elements smaller β left side
- Elements greater β right side
Examples
Example 1
Input:
arr = [4, 1, 3, 9, 7]
Output:
[1, 3, 4, 7, 9]
Example 2
Input:
arr = [2, 1, 6, 10, 4, 1, 3, 9, 7]
Output:
[1, 1, 2, 3, 4, 6, 7, 9, 10]
Core Idea of Quick Sort
π₯ Key Insight (Partition is Everything)
Quick Sort depends entirely on partitioning:
π After partition:
- Pivot is at its correct sorted position
- Left side β smaller elements
- Right side β larger elements
Intuition (Visual Understanding)
Consider:
Step 1: Choose Pivot
Letβs say pivot = 4
Step 2: Rearrange Elements
Now:
- Left β smaller
- Right β larger
Step 3: Apply Recursively
Final result:
Partition Logic (Most Important)
Your implementation uses:
- Pivot = first element
- Two pointers:
iβ moves forwardjβ moves backward
Java Code
Step-by-Step Dry Run
Input:
Execution:
- Pivot = 4
- i β moves until element > 4
- j β moves until element β€ 4
Swaps happen β pivot placed correctly
Final partition:
Complexity Analysis
Time Complexity
| Case | Complexity |
| Best Case | O(n log n) |
| Average Case | O(n log n) |
| Worst Case | O(nΒ²) |
Why Worst Case Happens?
When array is:
- Already sorted
- Reverse sorted
Pivot always becomes smallest/largest.
Space Complexity
- O(log n) (recursion stack)
β Common Mistakes
- Wrong partition logic
- Infinite loops in while conditions
- Incorrect pivot placement
- Not handling duplicates properly
β‘ Optimizations
1. Random Pivot
Avoid worst-case:
2. Median of Three
Choose better pivot:
Quick Sort vs Merge Sort
| Feature | Quick Sort | Merge Sort link to get more |
| Space | O(log n) | O(n) |
| Speed | Faster (practical) | Stable |
| Worst Case | O(nΒ²) | O(n log n) |
Why Quick Sort is Preferred
- Cache-friendly
- In-place sorting
- Faster in real-world scenarios
Key Takeaways
- Partition is the heart of Quick Sort
- Pivot must be placed correctly
- Recursion splits problem efficiently
- Avoid worst case using random pivot
When to Use Quick Sort
- Large arrays
- Memory constraints (in-place)
- Performance-critical applications
Conclusion
Quick Sort is one of the most efficient and practical sorting algorithms. Mastering its partition logic is crucial for solving advanced problems and performing well in coding interviews.
Understanding how pointers move and how pivot is placed will make this algorithm intuitive and powerful.
Frequently Asked Questions (FAQs)
1. Is Quick Sort stable?
No, it is not stable.
2. Why is Quick Sort faster than Merge Sort?
Because it avoids extra space and is cache-efficient.
3. What is the most important part?
π Partition logic




