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

前端 未结 4 1466
被撕碎了的回忆
被撕碎了的回忆 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:41

    Here is C# version of bottom-up-mergesort (for some more details you may refer to my blog @ http://dream-e-r.blogspot.com/2014/07/mergesort-arrays-and-lists.html)

    void BottomUpMergesort(int[] a)
            {
                int[] temp = new int[a.Length];
                for (int runWidth = 1; runWidth < a.Length; runWidth = 2 * runWidth)
                {
                    for (int eachRunStart = 0; eachRunStart < a.Length; 
                        eachRunStart = eachRunStart + 2 * runWidth)
                    {
                        int start = eachRunStart;
                        int mid = eachRunStart + (runWidth - 1);
                        if(mid >= a.Length)
                        {
                            mid = a.Length - 1;
                        }
                        int end = eachRunStart + ((2 * runWidth) - 1);
                        if(end >= a.Length)
                        {
                            end = a.Length - 1;
                        }
    
                        this.Merge(a, start, mid, end, temp);
                    }
                    for (int i = 0; i < a.Length; i++)
                    {
                        a[i] = temp[i];
                    }
                }
    

    And merge is defined as:

    void Merge(int[] a, int start, int mid, int end, int[] temp)
            {
                int i = start, j = mid+1, k = start;
                while((i<=mid) && (j<=end))
                {
                    if(a[i] <= a[j])
                    {
                        temp[k] = a[i];
                        i++;
                    }
                    else
                    {
                        temp[k] = a[j];
                        j++;
                    }
                    k++;
                }
                while(i<=mid)
                {
                    temp[k] = a[i];
                    i++;
                    k++;
                }
                while (j <= end)
                {
                    temp[k] = a[j];
                    j++;
                    k++;
                }
                Assert.IsTrue(k == end+1);
                Assert.IsTrue(i == mid+1);
                Assert.IsTrue(j == end+1);
            }
    
            }
    

    Just for reference here is the TopDownMergesort:

    void TopDownMergesort(int[] a, int[] temp, int start, int end)
            {
                if(start==end)
                {
                    //run size of '1'
                    return;
                }
                int mid = (start + end) / 2;
                this.TopDownMergesort(a, temp, start, mid);
                this.TopDownMergesort(a, temp, mid + 1, end);
                this.Merge(a, start, mid, end, temp);
                for(int i = start;i<=end;i++)
                {
                    a[i] = temp[i];
                }
            }
    

    UnitTests

    [TestMethod]
            public void BottomUpMergesortTests()
            {
                int[] a = { 13, 4, 1, 3, 8, 11, 9, 10 };
                this.BottomUpMergesort(a);
                int[] b = { 1, 3, 4, 8, 9, 10, 11, 13 };
                Assert.IsTrue(a.Length == b.Length);
                for (int i = 0; i < a.Length; i++)
                {
                    Assert.IsTrue(a[i] == b[i]);
                }
                List l = new List();
                for (int i = 10; i >= 1; i--)
                {
                    l.Add(i);
                }
                var la = l.ToArray();
                this.BottomUpMergesort(la);
                for (int i = 1; i <= 10; i++)
                {
                    Assert.IsTrue(la[i - 1] == i);
                }
                l.Clear();
                for (int i = 16; i >= 1; i--)
                {
                    l.Add(i);
                }
                la = l.ToArray();
                this.BottomUpMergesort(la);
                for (int i = 1; i <= l.Count; i++)
                {
                    Assert.IsTrue(la[i - 1] == i);
                }
            }
    

提交回复
热议问题