Non-recursive merge sort with two nested loops - how?

前端 未结 4 1471
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 12:24

First question here, and yes this is a homework question. We are tasked with performing merge sort on an array (which I am familiar with), but in a way I am unsure of how to do.

4条回答
  •  春和景丽
    2021-02-04 12:46

    It's not so difficult. Consider the recursive merge:

           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
                /     \               split
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
          /   \         /  \          split
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
      / \     / \     / \     / \     split
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
    | | | | | | | | | | | | | | | |
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
      \ /     \ /     \ /     \ /     merge
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
                \     /               merge
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
    

    If you notice, when you split, you don't really do anything. You just tell the recursive function to partially sort the array. Sorting the array consists of first sorting both halves and then merging it. So basically, what you have is this:

    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
    | | | | | | | | | | | | | | | |
    +-+ +-+ +-+ +-+ +-+ +-+ +-+ +-+
      \ /     \ /     \ /     \ /     merge
      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
                \     /               merge
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
    

    Now from here it should be obvious. You first merge elements of the array 2 by 2, then 4 by 4, then 8 by 8 etc. That is the outer for gives you 2, 4, 8, 16, 32, ... (which is what it calls size of the segment because the i of the loop contains that number) and the inner for (say with iterator j) goes over the array, i by i merging array[j...j+i/2-1] with array[j+i/2..j+i-1].

    I wouldn't write the code since this is homework.

    Edit: a picture of how the inner for works

    Imagine if i is 4, so you are at this stage:

      +-+-+  +-+-+  +-+-+  +-+-+
      | | |  | | |  | | |  | | |
      +-+-+  +-+-+  +-+-+  +-+-+
          \   /         \  /          merge
        +-+-+-+-+     +-+-+-+-+
        | | | | |     | | | | |
        +-+-+-+-+     +-+-+-+-+
    

    you will have a for that once gives you 0(which is 0*i) as j and then 4 (which is 1*i) as j. (if i was 2, you would have j going like 0, 2, 4, 6)

    Now, once you need to merge array[0..1] with array[2..3] (which is formulated by array[j..j+i/2-1] and array[j+i/2..j+i-1] with j = 0) and then array[4..5] with array[6..7] (which is formulated by the same formulas array[j...j+i/2-1] and array[j+i/2..j+i-1] because now j = 4) That is:

    i = 4:
           +-+-+-+-+-+-+-+-+
           | | | | | | | | |
           +-+-+-+-+-+-+-+-+
            ^ ^ ^ ^ ^ ^ ^ ^
            | | | | | | | |
           / / / /   \ \ \ \
         (j  =  0)   (j  =  4)
          | | | |     | | | |
          j | | |     j | | |
          | | | j+i-1 | | | j+i-1
          | | j+i/2   | | j+i/2
          | j+i/2-1   | j+i/2-1
          | | | |     | | | |
          | | | |     | | | |
          \ / \ /     \ / \ /
           v   v       v   v
           merge       merge
    

    Hope this is clear at least a little.


    Side help: Just a hint if you don't really know how for works:

    for (statement1; condition; statement2)
    {
        // processing
    }
    

    is like writing

    statement1;
    while (condition)
    {
        // processing
        statement2;
    }
    

    So, if you always wrote

    for (int i = 0; i < 10; ++i)
    

    it meant starting from 0, while i is smaller than 10, do something with i and then increment it. Now if you want i to change differently, you just change statement2 such as:

    for (int i = 1; i < 1024; i *= 2)
    

    (Try to understand how that final for works based on its equivalent while that I wrote you)

提交回复
热议问题