基数排序不起作用

时间:2017-10-23 18:47:35

标签: java arrays sorting radix-sort

我目前正在编写一个程序来比较MSD基数排序与使用Arrays.sort(数组)的QuicksSort相比的毫秒数。

如果基数排序中的输入不是那么大,那么它就像假设一样。但是如果我增加输入,算法将突然永远完成。我不知道为什么会这样,我似乎无法在任何地方找到解决方案。我知道一个事实是,与基数排序相比,大型阵列的快速排序速度较慢,而且由于某种原因,此程序只是在相反的方向上工作(快速排序变得更快,基数排序更慢)。

此时我觉得我已经尝试了所有方法来找到这个问题的答案,但我似乎无法找到错误的答案。

以下是代码:

    import java.util.*;

public class RadixSort{

  final static int NUM_BIT = 9;
  final static int MIN_NUM = 31;

  public static void main(String[] args){
    Random random = new Random();
    RadixSort mainObject = new RadixSort();


    int n = 100;
    double time = 0;
    double quicktime = 0;

    while(n != 10000000){

      int[] a = new int[n];
      for(int i = 0; i < a.length; i++){
        a[i] = random.nextInt(a.length);
      }

      time = mainObject.VRadixMulti(a);
      quicktime = mainObject.quickSort(a);

       System.out.println("\nQuicksort time for n = " + n + ": " + quicktime);

       System.out.println("RadixSort time for n = " + n + ": " + time);

       n = n*10;
    }
  }



  public double VRadixMulti(int[] a){
    long tt = System.nanoTime();
    int numBit = 0;
    int[] b = new int[a.length];


    int max = a[0];
    for(int i = 1; i < a.length; i++){
      if(a[i] > max){
        max = a[i];
      }
    }

    while(max >=1 << numBit){numBit++;}
    LeftRadix(a, b, 0, a.length, numBit, NUM_BIT);


    double time = (System.nanoTime()- tt)/1000000.0;
    testSort(a);
    return time;
  }


  public void LeftRadix(int[] a, int[] b, int left, int right, int leftSortBit, int maskLen){
    int mask = (1<<maskLen)-1;
    int shift = leftSortBit-maskLen;
    int acumVal = 0, j;
    int[] count = new int[mask+1];


    if(shift < 0){
      mask = (1<<maskLen)-1;
      shift = 0;
    }

    for(int i = left; i < right; i++){
      count[(a[i]>> shift) & mask]++;
    }

    for(int i = 0; i <= mask; i++){
      j = count[i];
      count[i] = acumVal;
      acumVal += j;
    }

    for(int i = 0; i < a.length; i++){
      b[count[(a[i]>>shift) & mask]++] = a[i];
    }

    int startIndex = left;
    int prevDig = (b[left] >> shift) & mask;

    if(shift >= 0){
      for(int i = left; i < right; i++){
        int val = (b[i] >> shift) & mask;

        if(i == right-1){
          if(i - startIndex <= NUM_BIT){
            insertSort(b, startIndex, i);
          } else {
            LeftRadix(b, a, startIndex, i, leftSortBit-maskLen, maskLen);
          }
        }

        if(val != prevDig){
          if(i-startIndex <= 1){
            prevDig = val;
            continue;
          }

          if(i - startIndex <= NUM_BIT){
            insertSort(b, startIndex, i);
          } else {
            LeftRadix(b, a, startIndex, i, leftSortBit-maskLen, maskLen);
          }
          startIndex = i;
        }
        prevDig = val;
      }
    }
    for(int i = 0; i < a.length; i++){
      a[i] = b[i];
    }
  }


  public void insertSort(int[] array, int left, int right){
    int temp;


    for(int i = 1; i < right; i++){
      for(int j = i; j > 0; j--){
        if(array[i] < array[i-1]){
          temp = array[j];
          array[j] = array[j-1];
          array[j-1] = temp;
        }
      }
    }
  }

  public double quickSort(int[] a){
    long time = System.nanoTime();
    Arrays.sort(a);
    double time = (System.nanoTime()- time)/1000000.0;
    return time;
  }


  public void testSort(int[] a){
    for (int i = 0; i< a.length-1;i++) {
      if (a[i] > a[i+1]){
        System.out.println("SorteringsFEIL på: "+
        i +" a["+i+"]:"+a[i]+" > a["+(i+1)+"]:"+a[i+1]);
      }
    }
  }
}

0 个答案:

没有答案