排序算法七:基数排序(Radix sort)

时间:2023-03-09 03:57:57
排序算法七:基数排序(Radix sort)

上一篇提到了计数排序,它在输入序列元素的取值范围较小时,表现不俗。但是,现实生活中不总是满足这个条件,比如最大整形数据可以达到231-1,这样就存在2个问题:

1)因为m的值很大,不再满足m=O(n),计数排序的时间复杂也就不再是线性的;

2)当m很大时,为计数数组申请的内存空间会很大;

为解决这两个问题,本篇讨论基数排序(Radix sort),基数排列的思想是:

1)将先按照某个基数将输入序列的每个元素划分成若*分,每个部分对排序结果的影响是有优先级的;

2)先按低优先级排序,再按高优先级排序,依次递推。这里要注意,每个部分进行排序时,必须选用稳定排序算法,例如基数排序。

3)最后的次序就是高优先级高的在前,高优先级相同的,低优先级高的在前。

(一)算法实现

     @Override
protected void sort(int[] toSort) {
// number to sort, n integers
int n = toSort.length;
// b bits each integer
int b = Integer.SIZE;
/*
* Split each integer into b/r digits, and each r bits long. So average
* running time is O(b/r(2^r+n)). It is proved that running time is
* close to least time while choosing r to lgn.
*/
int r = (int) Math.ceil(Math.log(n) / Math.log(2));
// considering the space cost, the maximum of r is 16.
r = Math.min(r, 16); int upperLimit = 1 << r;
int loopCount = b / r;
int j = 0;
int[] resultArray = new int[toSort.length];
int[] countingArray = new int[upperLimit];
while (j < loopCount) {
int rightShift = j * r;
radixSort(toSort, upperLimit, rightShift, resultArray,
countingArray);
Arrays.fill(countingArray, 0);
j++;
}
int mod = b % r;
if (mod != 0) {
upperLimit = 1 << mod;
int rightShift = r * loopCount;
countingArray = new int[upperLimit];
radixSort(toSort, upperLimit, rightShift, resultArray,
countingArray);
}
} private void radixSort(int[] toSort, int upperLimit, int rightShift,
int[] resultArray, int[] countingArray) {
int allOnes = upperLimit - 1;
for (int i = 0; i < toSort.length; i++) {
int radix = (toSort[i] >> rightShift) & allOnes;
countingArray[radix]++;
}
for (int i = 1; i < countingArray.length; i++) {
countingArray[i] += countingArray[i - 1];
} for (int i = toSort.length - 1; i >= 0; i--) {
int radix = (toSort[i] >> rightShift) & allOnes;
resultArray[countingArray[radix] - 1] = toSort[i];
countingArray[radix]--;
}
System.arraycopy(resultArray, 0, toSort, 0, resultArray.length);
}

radixSort

1)算法属于分配排序

2)平均时间复杂度是O(b/r(2r+n)), b-每个元素的bit数,r-每个元素划分成b/r个数字,每个数字r个bit。当r=log2n时,复杂度是O(2bn/log2n),也就是说,当b=O(log2n)时,时间复杂度是O(n).

3) 空间复杂度是O(2r+n)

4)算法属于稳定排序

(二)算法仿真

下面对随机化快速排序和基数排序,针对不同输入整数序列长度,仿真结果如下,从结果看,当输入序列长度越大,基数排序性能越优越。

**************************************************
Number to Sort is:2500
Array to sort is:{1642670374,460719485,1773719101,2140462092,1260791250,199719453,1290828881,1946941575,2032337910,643536338...}
Cost time of 【RadixSort】 is(milliseconds):48
Sort result of 【RadixSort】:{217942,491656,1389218,2642908,3608001,3976751,4905471,5094692,6340348,7693772...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):1
Sort result of 【RandomizedQuickSort】:{217942,491656,1389218,2642908,3608001,3976751,4905471,5094692,6340348,7693772...}
**************************************************
Number to Sort is:25000
Array to sort is:{987947608,1181521142,1240568028,373349221,289183678,2051121943,1257313984,745646081,1414556623,1859315040...}
Cost time of 【RadixSort】 is(milliseconds):1
Sort result of 【RadixSort】:{47434,109303,240122,255093,448360,526046,526445,628228,837987,966240...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):2
Sort result of 【RandomizedQuickSort】:{47434,109303,240122,255093,448360,526046,526445,628228,837987,966240...}
**************************************************
Number to Sort is:250000
Array to sort is:{1106960922,1965236858,1114033657,1196235697,2083563075,1994568819,1185250879,670222217,1386040268,1316674615...}
Cost time of 【RadixSort】 is(milliseconds):7
Sort result of 【RadixSort】:{466,884,8722,35382,37181,44708,53396,55770,67518,74898...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):27
Sort result of 【RandomizedQuickSort】:{466,884,8722,35382,37181,44708,53396,55770,67518,74898...}
**************************************************
Number to Sort is:2500000
Array to sort is:{1903738012,485657780,1747057138,2082998554,1658643001,91586227,2127717572,557705232,533021562,1322007386...}
Cost time of 【RadixSort】 is(milliseconds):81
Sort result of 【RadixSort】:{369,392,1316,1378,2301,3819,4013,4459,5922,6423...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):340
Sort result of 【RandomizedQuickSort】:{369,392,1316,1378,2301,3819,4013,4459,5922,6423...}
**************************************************
Number to Sort is:25000000
Array to sort is:{2145921976,298753549,11187940,410746614,503122524,1951513957,1760836125,2141838979,1702951573,1402856280...}
Cost time of 【RadixSort】 is(milliseconds):1,022
Sort result of 【RadixSort】:{130,145,406,601,683,688,736,865,869,954...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):3,667
Sort result of 【RandomizedQuickSort】:{130,145,406,601,683,688,736,865,869,954...}

相关源码:

 package com.cnblogs.riyueshiwang.sort;

 import java.util.Arrays;

 public class RadixSort extends abstractSort {

     @Override
protected void sort(int[] toSort) {
// number to sort, n integers
int n = toSort.length;
// b bits each integer
int b = Integer.SIZE;
/*
* Split each integer into b/r digits, and each r bits long. So average
* running time is O(b/r(2^r+n)). It is proved that running time is
* close to least time while choosing r to lgn.
*/
int r = (int) Math.ceil(Math.log(n) / Math.log(2));
// considering the space cost, the maximum of r is 16.
r = Math.min(r, 16); int upperLimit = 1 << r;
int loopCount = b / r;
int j = 0;
int[] resultArray = new int[toSort.length];
int[] countingArray = new int[upperLimit];
while (j < loopCount) {
int rightShift = j * r;
radixSort(toSort, upperLimit, rightShift, resultArray,
countingArray);
Arrays.fill(countingArray, 0);
j++;
}
int mod = b % r;
if (mod != 0) {
upperLimit = 1 << mod;
int rightShift = r * loopCount;
countingArray = new int[upperLimit];
radixSort(toSort, upperLimit, rightShift, resultArray,
countingArray);
}
} private void radixSort(int[] toSort, int upperLimit, int rightShift,
int[] resultArray, int[] countingArray) {
int allOnes = upperLimit - 1;
for (int i = 0; i < toSort.length; i++) {
int radix = (toSort[i] >> rightShift) & allOnes;
countingArray[radix]++;
}
for (int i = 1; i < countingArray.length; i++) {
countingArray[i] += countingArray[i - 1];
} for (int i = toSort.length - 1; i >= 0; i--) {
int radix = (toSort[i] >> rightShift) & allOnes;
resultArray[countingArray[radix] - 1] = toSort[i];
countingArray[radix]--;
}
System.arraycopy(resultArray, 0, toSort, 0, resultArray.length);
} public static void main(String[] args) {
for (int j = 0, n = 2500; j < 5; j++, n = n * 10) {
System.out
.println("**************************************************");
System.out.println("Number to Sort is:" + n);
int upperLimit = Integer.MAX_VALUE;
int[] array = CommonUtils.getRandomIntArray(n, upperLimit);
System.out.print("Array to sort is:");
CommonUtils.printIntArray(array); int[] array1 = Arrays.copyOf(array, n);
new RadixSort().sortAndprint(array1); int[] array2 = Arrays.copyOf(array, n);
new RandomizedQuickSort().sortAndprint(array2);
}
}
}

RadixSort.java