In learning the merge sort, the examples show the list of items being split in half over and over again and then merged again.
Why not just start with iterating over the individual item pairs in the list sequentially if the initial split action always splits the list into single individual items?
I understand that it is an iterative method vs recursive, but in this particular example it seems like the "divide" step will always seem break it up into single items so I'm a bit unclear about why we would even recursively divide when we can just divite it into single items in one step.
description of merge sort:
The mergeSort function should recursively sort the subarray array[p..r] i.e. after calling mergeSort(array,p,r) the elements from index p to index r of array should be sorted in ascending order.
-If the subarray has size 0 or 1, then it's already sorted, and so nothing needs to be done. -Otherwise, merge sort uses divide-and-conquer to sort the subarray.
Use merge(array, p, q, r) to merge sorted sub arrays array[p..q] and array[q+1..r].
js example
var mergeSort = function(array, p, r) {
if (r === p)
{
return array;
}
var q = p + floor((r - p)/2);
mergeSort(array, p, q);
mergeSort(array, q+1, r);
merge(array, p, q, r);
};