Introduction
Quick Sort is a:
- Divide and Conquer
- sorting algorithm
The idea is:
- choose a pivot
- place pivot correctly
- recursively sort partitions
Example:
Input: 5 2 4 1 3
Output:1 2 3 4 5
Explanation:
Array is partitioned around pivot elements and sorted recursively. This problem is one of the most important applications of:
Divide and Conquer Constraints
1 <= Array Size <= 10^5 Approach : Quick Sort Algorithm
Explanations:
Explanation:
The idea is:
- choose pivot element
- place smaller elements left
- place larger elements right
Steps:
- Choose pivot.
- Partition array.
- Place pivot correctly.
- Recursively sort left part.
- Recursively sort right part.
This approach:
- sorts in-place
- is very fast on average
Dry Run
Array:
5 2 4 1 3
Pivot:
3
Partition: 2 1 | 3 | 5 4
Sort Left:
1 2
Sort Right:
4 5
Final:1 2 3 4 5
Practice :