|
2 | 2 |
|
3 | 3 | --- |
4 | 4 |
|
5 | | -Natural merge sort is a variation of the merge sort algorithm that |
6 | | -takes advantage of the natural order present in the input array. |
7 | | -Natural merge sort scans through the array to identify naturally |
8 | | -occurring sorted sequences (runs) and merges consecutive pairs of |
| 5 | +Natural merge sort is a variation of the merge sort algorithm that |
| 6 | +takes advantage of the natural order present in the input array. |
| 7 | +Natural merge sort scans through the array to identify naturally |
| 8 | +occurring sorted sequences (runs) and merges consecutive pairs of |
9 | 9 | runs, until a single run remains. |
10 | 10 |
|
11 | | -The merge operation is not in-place - it requires O(n) extra space. |
12 | | -A simple solution (used here) is to merge the two sorted sub-arrays |
13 | | -to a temporary array then copy them back to the original array. |
14 | | -Merge uses three pointers/indices that scan from left to right; |
15 | | -one for each of the input sub-arrays and one for the output array. |
16 | | -At each stage the minimum input array element is copied to the |
17 | | -output array and the indices for those two arrays are incremented. |
18 | | -When one input array has been completely copied, any additional |
19 | | -elements in the other input array are copied to the output array. |
| 11 | +The merge operation is not in-place - it requires O(n) extra space. |
| 12 | +A simple solution (used here) is to merge the two sorted sub-arrays |
| 13 | +to a temporary array then copy them back to the original array. |
| 14 | +Merge uses three pointers/indices that scan from left to right; |
| 15 | +one for each of the input sub-arrays and one for the output array. |
| 16 | +At each stage the minimum input array element is copied to the |
| 17 | +output array and the indices for those two arrays are incremented. |
| 18 | +When one input array has been completely copied, any additional |
| 19 | +elements in the other input array are copied to the output array. |
20 | 20 |
|
21 | | -Natural merge sort is stable meaning the relative order of equal |
22 | | -elements is preserved. In the bottom-up merge sort, shown elsewhere, |
23 | | -the starting point assumes each run is one item long. In practice, |
24 | | -random input data will have many short runs that just happen to be |
25 | | -sorted. In the typical case, the natural merge sort may not need |
| 21 | +Natural merge sort is stable meaning the relative order of equal |
| 22 | +elements is preserved. In the bottom-up merge sort, shown elsewhere, |
| 23 | +the starting point assumes each run is one item long. In practice, |
| 24 | +random input data will have many short runs that just happen to be |
| 25 | +sorted. In the typical case, the natural merge sort may not need |
26 | 26 | as many passes because there are fewer runs to merge. |
0 commit comments