排序算法七:基数排序(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
排序算法七:基数排序(Radix sort)的更多相关文章
- 经典排序算法 - 基数排序Radix sort
经典排序算法 - 基数排序Radix sort 原理类似桶排序,这里总是须要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,临时忽视十位数 比如 待排序数组[ ...
- java排序算法之冒泡排序(Bubble Sort)
java排序算法之冒泡排序(Bubble Sort) 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一趟:首先比较第1个和第2个数 ...
- 桶排序/基数排序(Radix Sort)
说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...
- 基础排序算法之快速排序(Quick Sort)
快速排序(Quick Sort)同样是使用了分治法的思想,相比于其他的排序方法,它所用到的空间更少,因为其可以实现原地排序.同时如果随机选取中心枢(pivot),它也是一个随机算法.最重要的是,快速排 ...
- js 实现排序算法 -- 冒泡排序(Bubble Sort)
原文: 十大经典排序算法(动图演示) 冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来.走访数列的工作 ...
- 基数排序(radix sort)
#include<iostream> #include<ctime> #include <stdio.h> #include<cstring> #inc ...
- js 实现排序算法 -- 快速排序(Quick Sort)
原文: 十大经典排序算法(动图演示) 快速排序 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整 ...
- js 实现排序算法 -- 归并排序(Merge Sort)
原文: 十大经典排序算法(动图演示) 归并排序 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得 ...
- js 实现排序算法 -- 插入排序(Insertion Sort)
原文: 十大经典排序算法(动图演示) 插入排序 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描, ...
随机推荐
- 关于通过ip或者域名直接访问工程的问题
1. 上篇文章 在无界面centos7上部署jdk和tomcat 里介绍了在阿里服务器上部署javaweb工程,在部署完成后,我们需要通过 ip/域名:端口 的方式访问tomcat,但是在实际项目中, ...
- 【学习】020 Redis
Java缓存机制 Java中要用到缓存的地方很多,首当其冲的就是持久层缓存,针对持久层谈一下: 要实现java缓存有很多种方式,最简单的无非就是static HashMap,这个显然是基于内存缓存, ...
- 第二组_学生会管理系统_APP端个人感想
一:相关链接 1.相关源码链接: 1.学生会管理系统APP端:Code 2.学生会管理系统WEB端:Code 3.学生会管理系统后台:COde 2.相关文档和博客: 1.前期接口文档以及需求文档Doc ...
- gperftools尝试
最近在找windows下比较好用的函数时间统计的库,听同事说gperftools是跨平台的,就下载下来尝试了一把.发现它确实实现了windows上可以调用的dll库文件(tcmalloc_minima ...
- 组件通信 eventtBus
平级组件的通信 一个全局发布订阅模式,它是挂载到全局的 <!DOCTYPE html> <html lang="en"> <head> < ...
- java:集合输出之Iterator和ListIterator二
java:集合输出之Iterator和ListIterator二 ListIterator是Iterator的子接口,Iterator的最大特点是,能向前,或向后迭代.如果现在要想双向输出的话,则只能 ...
- java.lang.Void类源码解析_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 在一次源码查看ThreadGroup的时候,看到一段代码,为以下: /* * @throws NullPointerEx ...
- handy源码阅读(六):tcp类
首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...
- document.referer
参考文章: 深入理解document.referrer的用法
- android 摇一摇功能的实现
将这个功能封装成了一个类,这样今后方便调用 package com.bobo.myyaoyiyaotest; import android.R.bool; import android.content ...