Non-recursive merge sort with two nested loops - how?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Merge sort is a classic divide-and-conquer algorithm known for its efficiency and simplicity when it comes to sorting a list of elements. While typically implemented recursively, merge sort can also be implemented in a non-recursive manner using iterative techniques, specifically with two nested loops. This non-recursive approach not only helps in understanding the underlying mechanics of sorting but can also be useful in environments where recursion is undesirable or constrained.
Understanding the Non-Recursive Merge Sort
Overview
The basic idea of non-recursive merge sort is to iteratively merge subarrays of the input array until the entire array is merged and sorted. Unlike recursive merge sort, which divides the array into halves until each subarray has one element, the non-recursive approach starts with subarrays of a small fixed size and iteratively doubles this size until the entire array is sorted.
Algorithm Explanation
- Initialization:
- Start with subarray size
width = 1. - Iterate until
widthis less than the length of the array.
- Outer Loop (controls subarray size):
- For each pass through the array:
- Inner Loop (merging subarrays):
- Merge pairs of adjacent subarrays of the current
width.
- Merge Process:
- For pairs of subarrays, merge them into a single sorted array piece.
- Double the Subarray Size:
- Set
width = width * 2.
- Repeat the Process:
- Continue until
widthexceeds the length of the array.
Detailed Process
Example
Let's sort a small array using the non-recursive merge sort technique.
Consider an array: [38, 27, 43, 3, 9, 82, 10]
- Width = 1:
We merge individual elements into pairs:- Pairs:
[38, 27],[43, 3],[9, 82], and[10] - After merging:
[27, 38],[3, 43],[9, 82], and[10]
- Width = 2:
We merge the resulting arrays:- Pairs:
[27, 38]and[3, 43], then[9, 82]and[10] - After merging:
[3, 27, 38, 43]and[9, 10, 82]
- Width = 4:
Merge these two subarrays:- Finally merge
[3, 27, 38, 43]and[9, 10, 82] - Result:
[3, 9, 10, 27, 38, 43, 82]
The above steps illustrate how subarrays of increasing sizes are iteratively merged until the entire array is sorted.
Key Points
- Non-Recursive Nature: Avoids the overhead of function calls inherent in recursive implementations.
- Iterative merging: The sorted results of smaller subarrays are used to form larger sorted subarrays.
- Time Complexity: Remains , identical to the recursive version.
- Space Complexity: Typically due to temporary arrays needed during the merging process.
Benefits and Drawbacks
- Benefits:
- Avoids stack overflow issues related to deep recursion.
- Can be simpler to implement in environments with restricted support for recursion.
- Drawbacks:
- May involve additional logic to handle uneven subarrays at the array's tail.
Summary Table
| Aspect | Non-Recursive Merge Sort |
| Time Complexity | |
| Space Complexity | |
| Implementation | Uses iterative method with two nested loops |
| Initial Subarray Size | 1, then doubles every iteration |
| Optimal for | Large lists Environments with recursion constraints |
| Main Challenge | Managing index boundaries Handling last elements |
Additional Details
Potential Optimizations
- While the non-recursive merge sort works well for most arrays, optimizing the merge process or using insertion sort for very small subarrays can further improve performance.
- Another optimization can involve ensuring that merging takes place only if the last element of the first subarray is greater than the first element of the second subarray, as this can avoid unnecessary operations when segments are already sorted.
In conclusion, the non-recursive merge sort serves as an effective alternative to the recursive variant, maintaining the same efficiency while providing a solution suitable for non-recursive execution environments.
Related reading
- Non-trivial algorithm conversion from imperative to functional
- Normalizing the edit distance
- nth_element implementations complexities
- nth fibonacci number in sublinear time
- Not clear about the meaning of auto.offset.reset and enable.auto.commit in Kafka
- Number of commits and offset in each partition of a kafka topic
- Number of assignments necessary to find the minimum value in an array?
- Number of binary search trees over n distinct elements

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.