2

I've hit the books and was faced with problem i can't solve. I have been looking the information up a long. I broke my mind trying to understand it.

So, I'm given an array of length N (int) to sort it by using non-recursive merge sort algorithm. I learned merge sort algorithm for arrays of length 2^n. But I totally can't understand how it works for arrays of length N.

Can someone explain me how it works?

aristari
  • 25
  • 1
  • 5
  • Exactly the same. "Splitting the list in half" does not per se needs to be exactly in half. The "rounding errors" have no effect on the correctness. – Willem Van Onsem Oct 07 '19 at 17:26
  • Thanks, but i can't imagine the view of subtasks tree (level order) For example, A = {5,2,0,9}; root = [5,2, 0, 9] 1st level = [5,2] [0.9] 2nd level = [5][2] [0][9] 1) [5][2] merge 2) [0][9] merge 3) [2,5] and [0,9] merge 4) [0,2,5,9] is the result. but how it looks for N-arrays? (e.g. n=7) – aristari Oct 07 '19 at 17:44
  • you first split it in two lists, one with four elements, one with three elements, you sort the two lists, and then you merge these sorted lists. – Willem Van Onsem Oct 07 '19 at 17:48

2 Answers2

5

Given seven elements, the subtask tree looks like this:

          [3,5,2,1,4,7,6]
             /         \
      [3,5,2,1]        [4,7,6]
      /       \        /      \
   [3,5]    [2,1]    [4,7]    [6]
   /   \    /   \    /   \    /
 [3]  [5]  [2]  [1] [4]  [7] [6]

The idea is that you split the array "in half" at each level. If an array has an odd number of items, then splitting it will result in one subarray will have one more item than the other. It doesn't make the problem any more difficult: merging two arrays of different length is no trouble.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • The question is about non-recursive (bottom up) merge sort. – rcgldr Oct 09 '19 at 03:20
  • @rcgldr Bottom-up and top-down merge sort are the same thing. The only difference is that the iterative version doesn't use recursion to split the array. Rather, it uses nested loops. The actual tasks are the same. In my example, tasks are performed from the bottom up. – Jim Mischel Oct 09 '19 at 14:45
  • Bottom up doesn't "split" the array, it starts off by treating an array of N elements as N sorted runs of size 1, and immediately begins merging. Top down has to recursively split until two instances of run size == 1 are reached, and only does the merging begin to take place. Bottom up starts at the bottom level of your diagram. – rcgldr Oct 09 '19 at 15:05
  • @rcgldr Top-down doesn't "split" the array, either. It just uses recursion to determine the array indexes to use in the recursive merges. The only real difference is that the recursive version takes the array size and, using recursion, successively divides the sizes to come up with a list, in reverse order (a stack), of subarrays to merge. The non-recusrive version starts at 1 and performs the merges: the same merges that the recursive version does, but not in the same order. – Jim Mischel Oct 09 '19 at 15:45
0

A non recursive merge sort treats an array of N elements as N sorted runs of size 1, then merges even and odd runs from one array to another array. If there are an odd number of runs, the last one is just copied (nothing to merge it with), this may be taken care of in the merge() function. After a merge pass is done, the run size is doubled to 2, and the merge process is repeated. When the run size is doubled and is >= array size, the merge sort is done.

The code maintains 3 indexes: start of the left run, start of the right run (== end of left run), end of right run. While setting up or advancing the indexes, if an index becomes >= size of array, it's reduced to the size of the array. If this happens on the left run, then that is the last and odd run, so it is copied (again, this may be handled within a merge() function).

Wikipedia has pseudocode:

https://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation

rcgldr
  • 27,407
  • 3
  • 36
  • 61