Problem
Similar Sorts
Solution
Mergesort is one of the older algorithms known since 1945. It is very good example of divide and conquer paradigm and is better than other simpler sort algorithms like selection, insertion or bubble sort.
Method 1 - Recursive Top Down Merge Sort
Merge sort is a recursive algorithm that repeatedly calls itself on progressively smaller subsets of the problem.
Algorithm
We start by splitting the array into two halves, then sort each half, and finally merge them back together. This splitting continues until the base case of a single-element array is reached, allowing us to exit the recursion.
After reaching the base case, we merge the sorted halves to form the combined array. This merging step combines the two sorted arrays into a single sorted array.
So, this sums up merge sort algorithm:
- Divide: Split the array into two halves.
- Conquer: Recursively sort each half.
- Combine: Merge the two sorted halves into a single sorted array.
Here is a visual representation of merge sort:
Here is the video explanation:
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
, wheren
is number of elements. At each step in recursion, we are splitting array in two halves. This takesO(1)
time, but we are doing forlog n
levels. Then at each step, we are merging the sorted arrays that takesO(n)
time, but for all the levels it isO(log n * n)
- 🧺 Space complexity:
O(n)
, as our recursion stack goes log(n) levels, but at each level we haveO(n)
sized temporary array.
Method 2 - Iterative or Bottom up Merge Sort
This is iterative solution, of previous recursive method.
Code
|
|
Top Down vs Bottom up Merge Sort
Top down | Bottom up |
---|---|
Recursive | Non recursive |
It is top down, as we break the array into 2 halves, and these halves into further sub halves and so on, sort them and merge again | Here we dont break into halves, rather we have a step, which determines the size of the array to be broken. Initially it is one, we sort the array of size equal to step and go further |
Has more number of iterations | Has lesser number of iterations |
Professor Kevin wayne (of Princeton) replied that in many cases recursion is faster than iteration because of caching improved performances. | Algorithm wise it is as fast as top down, but may be slower as current implementation provide faster support for recurs |
Method 3 - In Place Recursive Merge Sort
In-place Merge Sort is a more challenging variant of the traditional Merge Sort algorithm because the standard merge operation inherently requires additional space proportional to the size of the array. However, it is possible to implement an in-place merge sort, although the implementation is complex and less common due to the complications involved in merging in place.
Here’s an outline of how you can achieve in-place merge sort with some additional complexity:
- Divide: Divide the array into two halves.
- Conquer: Recursively sort each half.
- In-place Combine: Merge the two sorted halves in place without using extra space.
Steps
- Divide: This step remains the same as the standard merge sort. Split the array into two halves.
- Recursive Sort: Recursively sort both halves.
- In-place Merge: To merge two sorted halves in place, you can use a technique known as the binary insertion method or rotate-and-swap operations to avoid additional space.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- 🧺 Space complexity:
O(1)
, The space complexity improves toO(1)
in terms of extra space for merging, but this also comes with increased complexity in the merging process.
Complexity Summary
Note | ||
---|---|---|
Best Time: | O(n log n) | Merge operation on every step |
Average Time: | O(n log n) | Same as above |
Worst Time: | O(n log n) | Same as above |
Stability: | Stable | |
In-place: | No | |
Best Space: | O(n) | Requires splitting list into new ones |
Average Space: | O(n) | Same as above |
Worst Space: | O(n) | Same as above |