views:

37

answers:

1

I am having problems with bottoms-up mergesort. I have problems sorting/merging. Current code includes:

   public void mergeSort(long[] a, int len) {
        long[] temp = new long[a.length];
        int length = 1;
        while (length < len) {
            mergepass(a, temp, length, len);
            length *= 2;
        }
    }


   public void mergepass(long[] a, long[] temp, int blocksize, int len) {
       int k = 0;
        int i = 1;
       while(i <= (len/blocksize)){
           if(blocksize == 1){break;}
           int min = a.length;
           for(int j = 0; j < blocksize; j++){
               if(a[i*j] < min){
                   temp[k++] = a[i*j];
                   count++;
               }
               else{
                   temp[k++] = a[(i*j)+1];
                   count++;
               }
           }
           for(int n = 0; n < this.a.length; n++){
               a[n] = temp[n];
           }
       }
    }
+1  A: 

Obvious problems:

  • i is never incremented.
  • At no point do you compare two elements in the array. (Is that what if(a[i*j] < min) is supposed to be doing? I can't tell.)
  • Why are you multiplying i and j?
  • What's this.a.length?

Style problems:

  • mergeSort() takes len as an argument, even though arrays have an implicit length. To make matters worse, the function also uses a.length and length.
  • Generally poor variable names.

Nitpicks:

  • If you're going to make a second array of the same size, it is common to make one the "source" and the other the "destination" and swap them between passes, instead of sorting into a temporary array and copying them back again.
tc.