Program work 16. Radix Sort in Java

不是基于比较的排序

是基于位数上的排序, 且仅适用于整数.

此位数的排序不会影响前一位数排序好的相对位置, 是stable的排序

影响效能的关键在于数组中最大数字的位数


最差时间复杂度为O(d*n).

另外需要O(n)space

d是最大数字的位数, 决定循环的轮数, n是每轮处理的数目的大小.


Pseudocode

radixSort (arr, tmp)
{
    get the max of the array
    d ←the digit number of max
    for (i ← digit; i > 0; i--) {
        resetCounter
        for each same digit position {
            counter[pos]++;
        }
        combineCounter;
        for j ← end; j >= 0; j-- {
            put the arr[j] to the tmp, pos is determined by the counter;
            counter[pos]--;
        }
}


Java

package ncku.cdc.sorting;

import java.util.Random;

public class RadixSort {
  private int[] sequence;
  private int[] counter;

  public RadixSort(int size, int range) {
    sequence = new int[size];
    counter = new int[10];
    Random rand = new Random();
    for (int i = 0; i < size; i++) {
      sequence[i] = rand.nextInt(range);
    }
  }

  public static void main(String[] args) {
    int size = Integer.valueOf(args[0]);
    int range = Integer.valueOf(args[1]);
    RadixSort radix = new RadixSort(size, range);

    System.out.println("before radixSort:");
    SortingTools.validation(radix.getSequence(), 0);

    int[] tmp = new int [size];
    radix.radixSort(radix.getSequence(), tmp, size);

    System.out.println("after radixSort:");
    SortingTools.validation(radix.getSequence(), 0);
  }

  public void radixSort(int[] arr, int[] tmp, int len) {
    int digit = getDigit(arr, len);
    int radix = 1;
    for (int d = digit; d > 0; d--) {
      resetCounter();
      int index;
      for (int i = 0; i < len; i++) {
        index = (arr[i] / radix) % 10;
        counter[index]++;
      }
      caculateCounter();
      for (int j = len - 1; j >= 0; j--) {
        index = (arr[j] / radix) % 10;
        tmp[--counter[index]] = arr[j];
      }
      radix *= 10;
      System.arraycopy(tmp, 0, arr, 0, len);
    }
  }

  private int getDigit(int[] arr, int len) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < len; i++) {
      if (arr[i] > max) {
        max = arr[i];
      }
    }
    int d = 1;
    while ((max /= 10) != 0) {
      d++;
    }
    return d;
  }

  private void resetCounter() {
    for (int i = 0; i < 10; i++) {
      counter[i] = 0;
    }
  }
  
  private void caculateCounter() {
    for (int i = 1; i < 10; i++) {
      counter[i] += counter[i - 1];
    }
  }

  public int[] getSequence() {
    return sequence;
  }
}


程序输入: 35 200. 表示生成一个长度35, 数字范围为[0, 200)的数组

程序输出:

before radixSort:
0 67 2 173 160 57 36 16 39 76 141 103 128 195 23 71 43 47 160 114 186 6 17 36 158 74 79 133 99 160 28 113 108 171 171 
after radixSort:
0 2 6 16 17 23 28 36 36 39 43 47 57 67 71 74 76 79 99 103 108 113 114 128 133 141 158 160 160 160 171 171 173 186 195 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值