Merge sort is an efficient, stable, and comparison-based sorting algorithm that uses a divide-and-conquer approach to sort data by recursively splitting arrays and then merging the sorted sub-arrays.
The Big Picture
Imagine you have a deck of cards that you want to sort. Merge sort is like dividing the deck into smaller piles, sorting each pile individually, and then merging the sorted piles back together to form a single, sorted deck.
Core Concepts
- Divide: Split the array into two halves.
- Conquer: Recursively sort each half.
- Merge: Combine the two sorted halves into a single sorted array.
Detailed Walkthrough
- Divide:
- Split the array into two roughly equal halves.
- Conquer:
- Recursively sort each half.
- Merge:
- Combine the sorted halves into a single sorted array by comparing the smallest elements of each half and arranging them in order.
Here's a step-by-step breakdown with an example:
Step-by-Step Example
Suppose we want to sort the array: [38, 27, 43, 3, 9, 82, 10]
.
- Divide:
- Split into
[38, 27, 43]
and[3, 9, 82, 10]
- Split into
- Conquer:
- Split
[38, 27, 43]
into[38]
and[27, 43]
- Split
[27, 43]
into[27]
and[43]
- Recursively sort these to get
[27, 38, 43]
- Split
[3, 9, 82, 10]
into[3, 9]
and[82, 10]
- Split
[3, 9]
into[3]
and[9]
- Split
[82, 10]
into[82]
and[10]
- Recursively sort these to get
[3, 9, 10, 82]
- Split
- Merge:
- Merge
[27, 38, 43]
and[3, 9, 10, 82]
:- Compare
27
and3
, put3
first - Compare
27
and9
, put9
next - Compare
27
and10
, put10
next - Compare
27
and82
, put27
next - Compare
38
and82
, put38
next - Compare
43
and82
, put43
next - Finally put
82
- Compare
- Resulting array:
[3, 9, 10, 27, 38, 43, 82]
- Merge
Understanding Through an Example
Let's merge two sorted arrays [27, 38, 43]
and [3, 9, 10, 82]
step by step:
- Compare the first elements:
27
and3
. Since3
is smaller, it's placed in the new array. - Compare
27
and9
.9
is smaller, so it's placed next. - Compare
27
and10
.10
is smaller, so it's placed next. - Compare
27
and82
.27
is smaller, so it's placed next. - Continue this process until all elements are merged into the new array.
Conclusion and Summary
Merge sort organizes data by:
- Splitting the array into smaller sub-arrays.
- Sorting each sub-array recursively.
- Merging the sorted sub-arrays back together.
This process ensures that the data is sorted efficiently, with a time complexity of (O(n \log n)), making it very effective for large datasets.
Test Your Understanding
- What are the main steps involved in merge sort?
- Explain how the merging process works in merge sort.
- Why is merge sort considered stable and efficient?
Reference
- "Introduction to Algorithms" by Cormen, Leiserson, Rivest, and Stein
- Khan Academy: Merge sort
'200===Dev Language > DS And Algorithm' 카테고리의 다른 글
Data Structures Introduced (0) | 2024.05.27 |
---|---|
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 |