Skip to content

Commit 61c35a9

Browse files
committed
feat: added extra info + explanation for natural merge sort
1 parent bbd8a6a commit 61c35a9

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/algorithms/explanations/msort_arr_bup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ it can also be adapted to sorting large quantities of data that do not
3434
fit into main memories. Historically, when data was primarily stored on
3535
magnetic tape, versions of bottom up merge sort were absolutely essential.
3636
There are recursive "top down" versions of merge sort, shown elsewhere.
37+
There is also a "natural" merge sort which utilises already sorted runs,
38+
also shown elsewhere
3739

src/algorithms/explanations/msort_arr_nat.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,25 @@
22

33
---
44

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+
runs, until a single run remains.
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.
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
26+
as many passes because there are fewer runs to merge.

src/algorithms/extra-info/msort_arr_nat.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ a:hover{
1515

1616
-----
1717

18-
Geeks for Geeks Link: [**Iterative Merge Sort**][G4GLink]
18+
Happy Coders Link: [**Natural Merge Sort**][HCLink]
1919

2020

21-
[G4GLink]: https://www.geeksforgeeks.org/iterative-merge-sort/
21+
[HCLink]: https://www.happycoders.eu/algorithms/merge-sort/#Natural_Merge_Sort
2222

0 commit comments

Comments
 (0)