How to sort in-place using the merge sort algorithm?

前端 未结 10 1593
心在旅途
心在旅途 2020-11-22 02:21

I know the question is not too specific.

All I want is someone to tell me how to convert a normal merge sort into an in-place merge sort (or a merge sort with const

相关标签:
10条回答
  • 2020-11-22 03:00

    This is my C version:

    void mergesort(int *a, int len) {
      int temp, listsize, xsize;
    
      for (listsize = 1; listsize <= len; listsize*=2) {
        for (int i = 0, j = listsize; (j+listsize) <= len; i += (listsize*2), j += (listsize*2)) {
          merge(& a[i], listsize, listsize);
        }
      }
    
      listsize /= 2;
    
      xsize = len % listsize;
      if (xsize > 1)
        mergesort(& a[len-xsize], xsize);
    
      merge(a, listsize, xsize);
    }
    
    void merge(int *a, int sizei, int sizej) {
      int temp;
      int ii = 0;
      int ji = sizei;
      int flength = sizei+sizej;
    
      for (int f = 0; f < (flength-1); f++) {
        if (sizei == 0 || sizej == 0)
          break;
    
        if (a[ii] < a[ji]) {
          ii++;
          sizei--;
        }
        else {
          temp = a[ji];
    
          for (int z = (ji-1); z >= ii; z--)
            a[z+1] = a[z];  
          ii++;
    
          a[f] = temp;
    
          ji++;
          sizej--;
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-22 03:01

    There is a relatively simple implementation of in-place merge sort using Kronrod's original technique but with simpler implementation. A pictorial example that illustrates this technique can be found here: http://www.logiccoder.com/TheSortProblem/BestMergeInfo.htm.

    There are also links to more detailed theoretical analysis by the same author associated with this link.

    0 讨论(0)
  • 2020-11-22 03:05

    Including its "big result", this paper describes a couple of variants of in-place merge sort (PDF):

    http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.22.5514&rep=rep1&type=pdf

    In-place sorting with fewer moves

    Jyrki Katajainen, Tomi A. Pasanen

    It is shown that an array of n elements can be sorted using O(1) extra space, O(n log n / log log n) element moves, and n log2n + O(n log log n) comparisons. This is the first in-place sorting algorithm requiring o(n log n) moves in the worst case while guaranteeing O(n log n) comparisons, but due to the constant factors involved the algorithm is predominantly of theoretical interest.

    I think this is relevant too. I have a printout of it lying around, passed on to me by a colleague, but I haven't read it. It seems to cover basic theory, but I'm not familiar enough with the topic to judge how comprehensively:

    http://comjnl.oxfordjournals.org/cgi/content/abstract/38/8/681

    Optimal Stable Merging

    Antonios Symvonis

    This paper shows how to stably merge two sequences A and B of sizes m and n, m ≤ n, respectively, with O(m+n) assignments, O(mlog(n/m+1)) comparisons and using only a constant amount of additional space. This result matches all known lower bounds...

    0 讨论(0)
  • 2020-11-22 03:08

    Just for reference, here is a nice implementation of a stable in-place merge sort. Complicated, but not too bad.

    I ended up implementing both a stable in-place merge sort and a stable in-place quicksort in Java. Please note the complexity is O(n (log n)^2)

    0 讨论(0)
  • 2020-11-22 03:09

    The critical step is getting the merge itself to be in-place. It's not as difficult as those sources make out, but you lose something when you try.

    Looking at one step of the merge:

    [...list-sorted...|x...list-A...|y...list-B...]

    We know that the sorted sequence is less than everything else, that x is less than everything else in A, and that y is less than everything else in B. In the case where x is less than or equal to y, you just move your pointer to the start of A on one. In the case where y is less than x, you've got to shuffle y past the whole of A to sorted. That last step is what makes this expensive (except in degenerate cases).

    It's generally cheaper (especially when the arrays only actually contain single words per element, e.g., a pointer to a string or structure) to trade off some space for time and have a separate temporary array that you sort back and forth between.

    0 讨论(0)
  • 2020-11-22 03:11

    Knuth left this as an exercise (Vol 3, 5.2.5). There do exist in-place merge sorts. They must be implemented carefully.

    First, naive in-place merge such as described here isn't the right solution. It downgrades the performance to O(N2).

    The idea is to sort part of the array while using the rest as working area for merging.

    For example like the following merge function.

    void wmerge(Key* xs, int i, int m, int j, int n, int w) {
        while (i < m && j < n)
            swap(xs, w++, xs[i] < xs[j] ? i++ : j++);
        while (i < m)
            swap(xs, w++, i++);
        while (j < n)
            swap(xs, w++, j++);
    }  
    

    It takes the array xs, the two sorted sub-arrays are represented as ranges [i, m) and [j, n) respectively. The working area starts from w. Compare with the standard merge algorithm given in most textbooks, this one exchanges the contents between the sorted sub-array and the working area. As the result, the previous working area contains the merged sorted elements, while the previous elements stored in the working area are moved to the two sub-arrays.

    However, there are two constraints that must be satisfied:

    1. The work area should be within the bounds of the array. In other words, it should be big enough to hold elements exchanged in without causing any out-of-bound error.
    2. The work area can be overlapped with either of the two sorted arrays; however, it must ensure that none of the unmerged elements are overwritten.

    With this merging algorithm defined, it's easy to imagine a solution, which can sort half of the array; The next question is, how to deal with the rest of the unsorted part stored in work area as shown below:

    ... unsorted 1/2 array ... | ... sorted 1/2 array ...
    

    One intuitive idea is to recursive sort another half of the working area, thus there are only 1/4 elements haven't been sorted yet.

    ... unsorted 1/4 array ... | sorted 1/4 array B | sorted 1/2 array A ...
    

    The key point at this stage is that we must merge the sorted 1/4 elements B with the sorted 1/2 elements A sooner or later.

    Is the working area left, which only holds 1/4 elements, big enough to merge A and B? Unfortunately, it isn't.

    However, the second constraint mentioned above gives us a hint, that we can exploit it by arranging the working area to overlap with either sub-array if we can ensure the merging sequence that the unmerged elements won't be overwritten.

    Actually, instead of sorting the second half of the working area, we can sort the first half, and put the working area between the two sorted arrays like this:

    ... sorted 1/4 array B | unsorted work area | ... sorted 1/2 array A ...
    

    This setup effectively arranges the work area overlap with the sub-array A. This idea is proposed in [Jyrki Katajainen, Tomi Pasanen, Jukka Teuhola. ``Practical in-place mergesort''. Nordic Journal of Computing, 1996].

    So the only thing left is to repeat the above step, which reduces the working area from 1/2, 1/4, 1/8, … When the working area becomes small enough (for example, only two elements left), we can switch to a trivial insertion sort to end this algorithm.

    Here is the implementation in ANSI C based on this paper.

    void imsort(Key* xs, int l, int u);
    
    void swap(Key* xs, int i, int j) {
        Key tmp = xs[i]; xs[i] = xs[j]; xs[j] = tmp;
    }
    
    /* 
     * sort xs[l, u), and put result to working area w. 
     * constraint, len(w) == u - l
     */
    void wsort(Key* xs, int l, int u, int w) {
        int m;
        if (u - l > 1) {
            m = l + (u - l) / 2;
            imsort(xs, l, m);
            imsort(xs, m, u);
            wmerge(xs, l, m, m, u, w);
        }
        else
            while (l < u)
                swap(xs, l++, w++);
    }
    
    void imsort(Key* xs, int l, int u) {
        int m, n, w;
        if (u - l > 1) {
            m = l + (u - l) / 2;
            w = l + u - m;
            wsort(xs, l, m, w); /* the last half contains sorted elements */
            while (w - l > 2) {
                n = w;
                w = l + (n - l + 1) / 2;
                wsort(xs, w, n, l);  /* the first half of the previous working area contains sorted elements */
                wmerge(xs, l, l + n - w, n, u, w);
            }
            for (n = w; n > l; --n) /*switch to insertion sort*/
                for (m = n; m < u && xs[m] < xs[m-1]; ++m)
                    swap(xs, m, m - 1);
        }
    }
    

    Where wmerge is defined previously.

    The full source code can be found here and the detailed explanation can be found here

    By the way, this version isn't the fastest merge sort because it needs more swap operations. According to my test, it's faster than the standard version, which allocates extra spaces in every recursion. But it's slower than the optimized version, which doubles the original array in advance and uses it for further merging.

    0 讨论(0)
提交回复
热议问题