O(n)
的排序以下排序算法都是针对特定场景才有优势的排序算法
以上场景一般具有以下特点:
要排序的数据有明显的范围。比如高考分数一般介于0~900
,公民身高一般介于0~3000
,公民体重一般介于0~150
,订单金额一般介于0~max(当年)
,而某一年的时间,如果把时间以时间戳形式计算,一定是介于某个固定区间的
既然有了明显的范围区间,就很方便划分区间段(类似统计中的柱状图),而且区间段的数据满足单调性,比如区间段f(x)
和区间段f(x+1)
一定满足如下关系:max(f(x)[i]) < min(f(x+1)[i])
,即上一个区间段内的最大值一定比下一个区间段的最小值要小(这里说的是升序排列)。比如高考分数可划分的区间段是0~100
、101~200
、201~300
、301~400
、401~500
、501~600
、601~700
、701~800
、801~900
,在区间段201~300
中的最大值很明显会小于区间段301~400
中的最小值。
划分好了区间段,还要满足各区间段内的数据量相对均匀。还拿高考分数为例,意思是区间段201~300
中的考生数和区间段301~400
中的考生数和…几乎相当。这个其实挺难的。因为很多类似的分布都会符合正态分布的,两头少,中间多,当然也有特定的情况,这完全是根据数据的业务属性而来的。但各区间段内的数据量相对均匀是桶排序很关键的一点。
为了满足各区间段内的数据量相对均匀,就需要根据实际业务场景,重新划分区间段(桶)。
比如考生的分数,公民的身高和体重,一般来说符合正态分布,仍以分数为例,这需要参考以往经验了,假设得到的经验是,一般会在500~800
之间扎堆儿,该区间考生数占据了所有的大约80%
,那么根据这条经验,可以做出新的桶划分。比如:0~500
、501~550
、551~600
、600~650
、651~700
、701~750
、751~800
、801~900
,或许500~800
还可以分的更细致些的。
比如微信的后台日志时间戳,以天为单位的话,假设得到的经验是,春节前后使用量很大,五一、十一也会很大。相应的调整新的桶划分。
以上的目的就是尽量满足各桶中的数据量均匀。
以空间换时间
尽可能减少数据间的比较
设置好若干个桶之后,首先遍历待排序数据,放入特定的桶中,时间复杂度是O(n)
,假设严丝合缝,也要开辟空间复杂度为O(n)
的多个桶做临时存储
接着将各桶中的数据分别排序,如果需要稳定性(好像没在排序里说明稳定性的意思,后边单说吧),使用归并排序,如果不需要稳定性,使用快速排序,总之单个桶的时间复杂度是O(klogk)
。假设设置的桶足够多,可以让一个桶里的元素值相等,那就不需要桶内排序了。但这需要开辟的桶空间可就更大了(这也算是把“以空间换时间”发挥到极致了吧)。
最后按序遍历各桶以及各桶中元素,依次放入原数组,完成排序。
比较纠结这个排序算法要怎么写合适,网上也没找到一个很通用的,毕竟设定几个桶,桶的深度都要结合实际情况来看的。这里只能放一个简单的例子,以及代码实现。
全班10人,身高分别是[150,163,158,166,170,169,158,175,181,175]
,按身高升序排序
在执行排序前,得知身高最大值max=181
,最小值min=150
,设置m=3
个桶,桶区间依次是150~160
,161~170
,171~181
,每个桶的深度为5
下边是伪代码
public void bucketSort(int[] array, int length) {
int bucketCount = 3;
int[] bucket1 = new int[5];
int bucket1Min = 150;
int bucket1Max = 160;
int[] bucket2 = new int[5];
int bucket2Min = 161;
int bucket2Max = 170;
int[] bucket3 = new int[5];
int bucket3Min = 171;
int bucket3Max = 181;
int bucket1Index = 0;
int bucket2Index = 0;
int bucket3Index = 0;
for(int i = 0; i < array.length; i++) {
int item = array[i];
if(item < bucket2Min) {
bucket1[bucket1Index++] = item;
} else if (item < bucket3Min) {
bucket2[bucket2Index++] = item;
} else {
bucket3[bucket3Index++] = item;
}
}
quickSort(bucket1, bucket1.length);
quickSort(bucket2, bucket2.length);
quickSort(bucket3, bucket3.length);
for(int i = 0; i < bucket1Index; i++) {
array[i] = bucket1[i];
}
for(int i = 0; i < bucket2Index; i++) {
array[bucket1Index + i] = bucket2[i];
}
for(int i = 0; i < bucket3Index; i++) {
array[bucket2Index + i] = bucket3[i];
}
}
场景和通排序的场景类似,只不过有了更苛刻的要求
要排序的数据不仅有明显的范围,而且数值范围不大。比如桶排序列出的诸多场景中,分数、身高、体重都是很好的;金额可能有点邪乎,主要是如果土豪太多,双十一一单花了好几个亿那种,区间就太大了,不过据说单个订单有金额上限,如果这样的话就很好了;按时间排序,这可能就有点头大了,但如果不是按纳秒,而是粗略一些忽略到毫秒(也难),忽略到秒(一年31,536,000
秒,也难),忽略到分钟(一年525,600
分钟,挺好),忽略到小时(一年8,760
小时,挺好),再忽略估计这个排序的意义可能就丧失了。
要将被排序的所有元素按照某个一对一映射的函数,先计算成非负整数。比如身高和体重,身高169.55
可以换算为16955
,换算公式是f(x) = 100 * x
;而x = f(x) / 100
;两个函数的计算结果都是唯一的。再比如时间排序,换算成时间戳,再减去一个待排序最小时间的时间戳,在允许的情况下忽略纳秒、毫秒、甚至秒(这个逆向的映射有些麻烦,但既然忽略了一些精度,就相当于按更粗略的单位来排序了)。
计数排序是特殊的桶排序,实现思路如下:
因为对数据值的范围做了限定:一方面不大,另一方面都可转为非负整数,那么就可以进行如下操作了。
假设数据值的最大值是max
,接着设定max+1
个桶,而这里的桶不用来存储元素的集合,而用来存储元素的个数,所以每个桶的长度为1
。
所以可创建一个长度为max+1
的数组充当这max+1
个桶
int[] temp = new int[max+1];
接着遍历原数组array
,统计每个值的个数,并将结果保存到桶中
for(int i = 0; i < array.length; i++) {
int item = array[i];
temp[item] = temp[item] + 1;
}
举个例子,数组array = {3,5,7,2,4,2,7,3,1}
其中最大元素是7
,那么桶就是temp = new int[8]
遍历array
index = 0; item = 3; temp[3] = temp[3] + 1; temp = {0,0,0,1,0,0,0,0};
index = 1; item = 5; temp[5] = temp[5] + 1; temp = {0,0,0,1,0,1,0,0};
index = 2; item = 7; temp[7] = temp[7] + 1; temp = {0,0,0,1,0,1,0,1};
index = 3; item = 2; temp[2] = temp[2] + 1; temp = {0,0,1,1,0,1,0,1};
index = 4; item = 4; temp[4] = temp[4] + 1; temp = {0,0,1,1,1,1,0,1};
index = 5; item = 2; temp[2] = temp[2] + 1; temp = {0,0,2,1,1,1,0,1};
index = 6; item = 7; temp[7] = temp[7] + 1; temp = {0,0,2,1,1,1,0,2};
index = 7; item = 3; temp[3] = temp[3] + 1; temp = {0,0,2,2,1,1,0,2};
index = 8; item = 1; temp[1] = temp[1] + 1; temp = {0,1,2,2,1,1,0,2};
最终得到temp = {0,1,2,2,1,1,0,2}
从数组temp
可以看出,数组array
中元素i
的个数是temp[i]
而如果希望数组中元素值所表达如下含义呢?
从数组temp
可以看出,数组array
中<= i
的元素的个数是temp[i]
数组temp
要做如下的元素叠加处理
for(int i = 1; i < temp.length; i++) {
temp[i] = temp[0] + temp[i];
}
最终得到temp = {0,1,3,5,6,7,7,9}
从数组temp
可以看出,数组array
中<= i
的元素个数是temp[i]
原array = {3,5,7,2,4,2,7,3,1}
,设置一个等长的临时数组result = new int[array.length]
第一个元素3
,得到temp[3] = 5
,说明<= 3
的元素个数是5
,那么经过排序后的array
中index = 4
(第5
个元素)的位置上放置的一定就是3
,所以result[4] = 3
。既然3
的位置已经确定,<= 3
的元素个数就是4
了,需要执行temp[3] = temp[3] - 1
此时result = {0,0,0,0,3,0,0,0,0}; temp = {0,1,3,4,6,7,7,9};
第二个元素5
,得到temp[5] = 7
,说明<= 5
的元素个数是7
,那么经过排序后的array
中index = 6
(第7
个元素)的位置上放置的一定就是5
,所以result[6] = 5
。同样,temp[5] = temp[5] - 1
此时result = {0,0,0,0,3,0,5,0,0}; temp = {0,1,3,4,6,6,7,9};
第三个元素7
此时result = {0,0,0,0,3,0,5,0,7}; temp = {0,1,3,4,6,6,7,8};
第四个元素2
此时result = {0,0,2,0,3,0,5,0,7}; temp = {0,1,2,4,6,6,7,8};
第五个元素4
此时result = {0,0,2,0,3,4,5,0,7}; temp = {0,1,2,4,5,6,7,8};
第六个元素2
此时result = {0,2,2,0,3,4,5,0,7}; temp = {0,1,1,4,5,6,7,8};
第七个元素7
此时result = {0,2,2,0,3,4,5,7,7}; temp = {0,1,1,4,5,6,7,7};
第八个元素3
此时result = {0,2,2,3,3,4,5,7,7}; temp = {0,1,1,3,5,6,7,7};
第九个元素1
此时result = {1,2,2,3,3,4,5,7,7}; temp = {0,0,1,3,5,6,7,7};
最终得到数组result = {1,2,2,3,3,4,5,7,7}
就是排序结果
再将其逐个复制到原数组array
中即可
public void countingSort(int[] array, int length) {
// 找最大值
int max = array[0];
for (int i = 1; i < length; i++) {
if (max < array[i]) {
max = array[i];
}
}
// 创建统计元素个数的数组
int[] temp = new int[max + 1];
for (int i : array) {
temp[i] = temp[i] + 1;
}
// 叠加元素个数
for (int i = 1; i < temp.length; i++) {
temp[i] = temp[i - 1] + temp[i];
}
// 创建存储结果的临时数组
int[] result = new int[length];
for (int i : array) {
result[temp[i] - 1] = i;
temp[i] = temp[i] - 1;
}
// 将result拷贝到array中去
for (int i = 0; i < array.length; i++) {
array[i] = result[i];
}
}
@Test
public void countingSort() {
int[] array = {2,6,7,3,1,5,3,2,3};
System.out.println(Arrays.toString(array));
countingSort(array,array.length);
System.out.println(Arrays.toString(array));
}
采坑啦!
其实是在写基数排序时,使用了上述计数排序,就踩到坑了
坑的位置在
// 创建存储结果的临时数组
int[] result = new int[length];
for (int i : array) {
result[temp[i] - 1] = i;
temp[i] = temp[i] - 1;
}
针对重复元素,因为temp[i]
要减一,所以后插入的元素是在先插入的前边
而这段代码中是按array
从前向后遍历的,这和上述很巧妙的方式正好反向,导致这里的排序不是稳定的排序
修改方案就是改为逆向遍历
// 创建存储结果的临时数组
int[] result = new int[length];
for (int i = length - 1; i >= 0; i--) {
int item = array[i];
result[temp[item] - 1] = item;
temp[item] = temp[item] - 1;
}
完整代码如下:
public void countingSort(int[] array, int length) {
// 找最大值
int max = array[0];
for (int i = 1; i < length; i++) {
if (max < array[i]) {
max = array[i];
}
}
// 创建统计元素个数的数组
int[] temp = new int[max + 1];
for (int i : array) {
temp[i] = temp[i] + 1;
}
// 叠加元素个数
for (int i = 1; i < temp.length; i++) {
temp[i] = temp[i - 1] + temp[i];
}
// 创建存储结果的临时数组
int[] result = new int[length];
for (int i = length - 1; i >= 0; i--) {
int item = array[i];
result[temp[item] - 1] = item;
temp[item] = temp[item] - 1;
}
// 将result拷贝到array中去
for (int i = 0; i < array.length; i++) {
array[i] = result[i];
}
}
@Test
public void countingSort() {
int[] array = {2, 6, 7, 3, 1, 5, 3, 2, 3};
System.out.println(Arrays.toString(array));
countingSort(array, array.length);
System.out.println(Arrays.toString(array));
}
这里涉及到的都是字符串排序,而且满足如下条件:
第一个字符小的排在前
第一个字符相同时,第二个字符小的排在前
第一第二个字符相同时,第三个字符小的排在前
…
思路其实是没有的,只是觉得很巧吧
主要是执行固定次数的时间复杂度为O(n)
的稳定排序
比如如下5
个字符串
["113","217","121","212","221"]
首先按第三位排序得
["121","221","212","113","217"]
注意这里是稳定排序(当然,在这里还用不到稳定排序的优势),121
和221
顺序不变
接着按第二位排序得
["212","113","217","121","221"]
再次注意这里的稳定排序
最后按首位排序得
["113","121","212","217","221"]
private void countingSort(String[] array, int length, int index) {
char max = array[0].charAt(index);
for (int i = 1; i < length; i++) {
char c = array[i].charAt(index);
if (max < c) {
max = c;
}
}
int maxValue = charToInt(max);
int[] temp = new int[maxValue + 1];
for (int i = 0; i < length; i++) {
int item = charToInt(array[i].charAt(index));
temp[item] = temp[item] + 1;
}
for (int i = 1; i < temp.length; i++) {
temp[i] = temp[i - 1] + temp[i];
}
String[] result = new String[length];
for (int i = length - 1; i >= 0; i--) {
int item = charToInt(array[i].charAt(index));
result[temp[item] - 1] = array[i];
temp[item] = temp[item] - 1;
}
for (int i = 0; i < array.length; i++) {
array[i] = result[i];
}
}
private int charToInt(char c) {
return c - '0';
}
public void radixSort(String[] array, int length) {
int stringLength = array[0].length();
for (int i = stringLength - 1; i >= 0; i--) {
countingSort(array, length, i);
}
}
@Test
public void radixSort() {
String[] array = {"12345678901", "16789012342", "17890123453", "13456789014", "11234567895",
"15678901236", "13456789017", "12345678908", "13456789019"};
System.out.println(Arrays.toString(array));
radixSort(array, array.length);
System.out.println(Arrays.toString(array));
}
这里附上一段经某位同学点拨后写出的代码,适用于亿级别int
类型整数的高速排序
public void radixSort(int[] array, int length) {
for (int i = 0; i < 32; i++) {
radixSort(array, length, i);
}
}
private void radixSort(int[] array, int length, int index) {
int[] tempArray = new int[2];
for (int i = 0; i < array.length; i++) {
int item = (array[i] >> index) & 1;
tempArray[item] = tempArray[item] + 1;
}
for (int i = 1; i < tempArray.length; i++) {
tempArray[i] = tempArray[i] + tempArray[i - 1];
}
int[] result = new int[length];
for (int i = length - 1; i >= 0; i--) {
int item = (array[i] >> index) & 1;
result[tempArray[item] - 1] = array[i];
tempArray[item] = tempArray[item] - 1;
}
for (int i = 0; i < length; i++) {
array[i] = result[i];
}
}
说明一下:
这相当于把一个int
类型值先转为二进制,再按二进制排序。int
最多32
位
上万级别的数据,某位上的最大值为0
的概率极低,所以最大值为1
,存储0
和1
的个数数组就是int[2]
了。
其余操作不变,但对于取某位上的数值,可使用如下公式:
取二进制数的倒数第i
位的值 x >> (i - 1) & 1