Quick sort is an efficient sorting algorithm that uses a divide-and-conquer approach to organize data by partitioning arrays and recursively sorting the partitions.
The Big Picture
Imagine you have a messy room full of books, and you want to organize them on a shelf. Quick sort is like picking a random book (called the "pivot"), then arranging all other books into two piles: those that should come before the pivot and those that should come after. You repeat this process for each pile until the whole room is sorted.
Core Concepts
- Pivot Selection: Choosing an element from the array as the pivot.
- Partitioning: Rearranging the array so that elements less than the pivot come before it, and elements greater come after it.
- Recursion: Applying the same process to the sub-arrays (or piles) created by partitioning.
Detailed Walkthrough
- Choose a Pivot: Select an element from the array. This can be the first element, last element, or a random element.
- Partition the Array:
- Move elements smaller than the pivot to the left of the pivot.
- Move elements larger than the pivot to the right.
- The pivot element is now in its correct position.
- Recursively Sort Sub-arrays: Apply the same process to the left and right sub-arrays around the pivot.
Here's a step-by-step breakdown with an example:
Step-by-Step Example
Suppose we want to sort the array: [8, 3, 1, 7, 0, 10, 2]
.
- Choose a Pivot: Let's pick
7
. - Partitioning:
- Elements less than
7
:[3, 1, 0, 2]
- Elements greater than
7
:[8, 10]
- Resulting array:
[3, 1, 0, 2, 7, 8, 10]
- Elements less than
- Recursively Apply Quick Sort:
- Left sub-array
[3, 1, 0, 2]
:- Choose pivot:
2
- Partition:
[1, 0]
,[3]
- Resulting array:
[1, 0, 2, 3]
- Apply quick sort on
[1, 0]
:- Pivot:
0
- Partition:
[]
,[1]
- Resulting array:
[0, 1]
- Pivot:
- Choose pivot:
- Right sub-array
[8, 10]
:- Pivot:
10
- Partition:
[8]
,[]
- Resulting array:
[8, 10]
- Pivot:
- Left sub-array
After sorting all partitions, we combine them to get the final sorted array: [0, 1, 2, 3, 7, 8, 10]
.
Conclusion and Summary
Quick sort organizes data efficiently by:
- Selecting a pivot.
- Partitioning the array into two sub-arrays based on the pivot.
- Recursively sorting the sub-arrays.
By continually dividing the problem and conquering smaller sub-problems, quick sort can sort an array with an average time complexity of (O(n \log n)).
Test Your Understanding
- What is the purpose of the pivot in quick sort?
- Describe the partitioning process in your own words.
- How does the recursive nature of quick sort help in sorting an array?
Reference
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
- Khan Academy: Quick sort
'200===Dev Language > DS And Algorithm' 카테고리의 다른 글
Recursion Introduced (0) | 2024.05.27 |
---|---|
Merge Sort Introduced (0) | 2024.05.27 |
Insertion Sort Introduced (0) | 2024.05.27 |
Bellman-Ford Algorithm (0) | 2024.05.27 |
Dijkstra's Algorithm Introduced (0) | 2024.05.27 |