Is Merge Sort implementation wrong in Wikipedia?
https://en.wikipedia.org/wiki/Merge_sort
In the below code:
1.
iMiddle = (iEnd - iBegin) / 2
should be iMiddle = iBegin + (iEnd - iBegin) / 2
I guess.
2. In method TopDownMerge
array B was sorted but never copied to array A.
Merge sort
In computer science, merge sort (also commonly spelled as mergesort and as merge-sort) is an efficient, general-purpose, and comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the relative order of equal elements is the same in the input and output. Merge sort is a divide-and-conquer algorithm that w...
3 Replies
its
(iEnd + iBegin) / 2
which is equivalent to iBegin + (iEnd - iBegin) / 2
not sure what you mean by 2
A is being sorted into BI am so blind. Thank you.
I was thinking of copying array B to A after sorting to then use the new partly sorted array A in further steps of recursion instead of using completely unsorted A each time.
you dont want to mutate A
you either do the sorting in place or you return a new array and keep the original as is
thats typically how functions work