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

前端 未结 10 1735
心在旅途
心在旅途 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条回答
  •  萌比男神i
    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--;
        }
      }
    }
    

提交回复
热议问题