原文:https://www.cnblogs.com/onepixel/articles/7674659.html
一、算法概述:
1.算法分类:
十种常见算法可以分为两大类:
- 比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能超过Q(nlogn),因此也称为非线性时间比较类排序。
- 非比较类排序:不通过比较来决定元素间的相对次序,它可以突破基于比较排序的时间下限, 以线性时间运行, 因此也称为线性时间非比较类排序。
![](https://img-blog.csdnimg.cn/20190517115044833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMzk3ODE1,size_16,color_FFFFFF,t_70)
2.算法复杂度:
算法复杂度
排序方法 | 时间复杂度(平均) | 时间复杂度(最坏) | 时间复杂度(最好) | 空间复杂度 | 稳定性 |
插入排序 | O(n2) | O(n2) | O(n) | O(1) | 稳定 |
希尔排序 | O(n1.3) | O(n2) | O(n) | O(1) | 不稳定 |
选择排序 | O(n2) | O(n2) | O(n2) | O(1) | 不稳定 |
堆排序 | O(nlog2n) | O(nlog2n) | O(nlog2n) | O(1) | 不稳定 |
冒泡排序 | O(n2) | O(n2) | O(n) | O(1) | 稳定 |
快速排列 | O(nlog2n) | O(n2) | O(nlog2n) | O(nlog2n) | 不稳定 |
归并排序 | O(nlog2n) | O(nlog2n) | O(nlog2n) | O(n) | 稳定 |
计数排序 | O(n+k) | O(n+k) | O(n+k) | O(n+k) | 稳定 |
桶排序 | O(n+k) | O(n2) | O(n) | O(n+k) | 稳定 |
计数排序 | O(n*k) | O(n*k) | O(n*k) | O(n+k) | 稳定 |
3.相关概念:
-
稳定:如果a 原本在 b 前面,而a=b, 排序之后a 任然在b前面。
-
不稳定:如果 a 原本在 b 前面,而 a = b, 排序之后 a 可能会出现在 b 的后面。
-
时间复杂度:对排列数据的总的操作次数。反映当 n 变化时,操作次数呈现什么规律。
-
空间复杂度:是指算法在计算机内执行时所需存储空间的度量,它也是数据规模 n 的函数。
4.算法的选择:
-
若n较小( 如n≤50), 可采用直接插入或直接选择排序;
-
若文件初始状态基本有序(指正序),则应选用直接插入、冒泡或随机的快速排序为宜;
-
若n较大,则应采用时间复杂度为O(n log n) 的排序方法:快速排序、堆排序或归并排序;
-
若n较小,考虑稳定性,可以使用:基数排序、计数排序或者桶排序。
其中 插入算法和 归并算法 对重复率比较高的排序比较友好;冒泡算法不适合大量数据。
二、具体定义:
1.冒泡排序:
* 概念:冒泡排序是一种简单的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。
*算法描述:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142259572.gif)
*算法实例:
function bubbleSort(arr) {
var len = arr.length;
for (var i = 0; i < len - 1; i++) {
for (var j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) { // 相邻元素两两对比
var temp = arr[j + 1]; // 元素交换
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
var arr = [6, 5, 4, 3, 2, 1];
console.log(arr); // [6, 5, 4, 3, 2, 1]
bubbleSort(arr);
console.log(arr); // [1, 2, 3, 4, 5, 6]
2.选择排序:
*概念: 选择排序(Selection-sort)是一种简单直观的排序算法。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。
表现最稳定的排序算法之一,因为无论什么数据进去都是O(n2)的时间复杂度,所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。理论上讲,选择排序可能也是平时排序一般人想到的最多的排序方法了吧。
*算法描述:
n个记录的直接选择排序可经过n-1趟直接选择排序得到有序结果。具体算法描述如下:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142411197.gif)
*算法实例:
function selectionSort(arr) {
if (arr == null || arr.length < 2) {
return arr;
}
for (var i = 0; i < (arr.length - 1); i++) {
let minIndex = i;
for (let j = i + 1; j < arr.length; j++) {
minIndex = arr[j] < arr[minIndex] ? j : minIndex;
}
let temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
var arr = [1, 3, 2, 8, 9, 1, 5];
console.log(arr); // [1, 3, 2, 8, 9, 1, 5]
selectionSort(arr);
console.log(arr); // [1, 1, 2, 3, 5, 8, 9]
3.插入排序:
*概念:插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。
插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。
*算法描述:
一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:
-
从第一个元素开始,该元素可以认为已经被排序;
-
取出下一个元素,在已经排序的元素序列中从后向前扫描;
-
如果该元素(已排序)大于新元素,将该元素移到下一位置;
-
重复步骤3,直到找到已排序的元素小于或者等于新元素的位置;
-
将新元素插入到该位置后;
-
重复步骤2~5。
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142521855.gif)
*算法实例:
function insertionSort(arr) {
if (arr == null || arr.length < 2) {
return arr;
}
for (let i = 1; i < arr.length; i++) {
for (let j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
return arr;
}
var arr = [3, 4, 2, 1, 6, 7, 8, 4];
console.log(arr); // [3, 4, 2, 1, 6, 7, 8, 4]
insertionSort(arr);
console.log(arr); // [1, 2, 3, 4, 4, 6, 7, 8]
4.希尔排序:
*概念:1959年Shell发明,第一个突破O(n2)的排序算法,是简单插入排序的改进版。它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序。
希尔排序的核心在于间隔序列的设定。既可以提前设定好间隔序列,也可以动态的定义间隔序列。动态定义间隔序列的算法是《算法(第4版)》的合著者Robert Sedgewick提出的。
*算法描述:
先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,具体算法描述:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142611755.gif)
*算法实例:
function shellSort(arr) {
var len = arr.length;
for (var gap = Math.floor(len / 2); gap > 0; gap = Math.floor(gap /2)) {
for (var i = gap; i < len; i++) {
for (var j = i - gap; j >= 0 && arr[j] > arr[gap + j]; j -= gap) {
var temp = arr[j];
arr[j] = arr[gap + j];
arr[gap + j] = temp;
}
}
}
}
var arr = [49, 38, 65, 97, 76, 13, 27, 49, 55, 04];
console.log(arr); // [49, 38, 65, 97, 76, 13, 27, 49, 55, 4]
shellSort(arr);
console.log(arr); // [4, 13, 27, 38, 49, 49, 55, 65, 76, 97]
5.归并算法:
*概念:归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。
归并排序是一种稳定的排序方法。和选择排序一样,归并排序的性能不受输入数据的影响,但表现比选择排序好的多,因为始终都是O(nlogn)的时间复杂度。代价是需要额外的内存空间。
*算法描述:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142701685.gif)
*算法实例:
function mergeSort(arr) {
if (arr.length == 1) {
return arr;
}
var mid = Math.floor(arr.length / 2);
var leftArr = arr.slice(0, mid);
var rightArr = arr.slice(mid);
return merge(mergeSort(leftArr), mergeSort(rightArr));
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> merge(leftArr, rightArr) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> [];
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (leftArr.length &&<span style="color: rgba(0, 0, 0, 1)"> rightArr.length) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (leftArr[0] < rightArr[0<span style="color: rgba(0, 0, 0, 1)">]) {
result.push(leftArr.shift());
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
result.push(rightArr.shift());
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> result.concat(leftArr).concat(rightArr);
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> arr = [6, 4, 2, 1, 12, 4, 6, 5, 5, 0<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [6, 4, 2, 1, 12, 4, 6, 5, 5, 0]</span>
console.log(mergeSort(arr)); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [0, 1, 2, 4, 4, 5, 5, 6, 6, 12]</span></pre></code-box>
6.快速排序:
*概念:快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序。
*算法描述:
快速排序使用分治法来把一个串(list)分为两个子串(sub-lists)。具体算法描述如下:
-
从数列中挑出一个元素,称为 “基准”(pivot);
-
重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作;
-
递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142745730.gif)
*算法实例:
const quickSort = (array) => {
const sort = (arr, left = 0, right = arr.length - 1) => {
if (left >= right) { //如果左边的索引大于等于右边的索引说明整理完毕
return
}
let i = left
let j = right
const baseVal = arr[j] // 取无序数组最后一个数为基准值
while (i < j) { //把所有比基准值小的数放在左边大的数放在右边
while (i < j && arr[i] <= baseVal) { //找到一个比基准值大的数交换
i++
}
arr[j] = arr[i] // 将较大的值放在右边如果没有比基准值大的数就是将自己赋值给自己(i 等于 j)
while (j > i && arr[j] >= baseVal) { //找到一个比基准值小的数交换
j--
}
arr[i] = arr[j] // 将较小的值放在左边如果没有找到比基准值小的数就是将自己赋值给自己(i 等于 j)
}
arr[j] = baseVal // 将基准值放至中央位置完成一次循环(这时候 j 等于 i )
sort(arr, left, j - 1) // 将左边的无序数组重复上面的操作
sort(arr, j + 1, right) // 将右边的无序数组重复上面的操作
}
const newArr = array.concat() // 为了保证这个函数是纯函数拷贝一次数组
sort(newArr)
return newArr
}
var arr = [6,5,4,5,3,4,1];
console.log(arr); // [6, 5, 4, 5, 3, 4, 1]
console.log(quickSort(arr)); // [1, 3, 4, 4, 5, 5, 6]
7.堆排序:
*概念:堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。
*算法描述:
-
将初始待排序关键字序列(R1,R2….Rn)构建成大顶堆,此堆为初始的无序区;
-
将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,……Rn-1)和新的有序区(Rn),且满足R[1,2…n-1]<=R[n];
-
由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,……Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2….Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142825891.gif)
*算法实例:
function heapSort(arr) {
var len; // 因为声明的多个函数都需要数据长度,所以把len设置成为全局变量
buildMaxHeap(arr);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = arr.length - 1; i > 0; i--<span style="color: rgba(0, 0, 0, 1)">) {
swap(arr, </span>0<span style="color: rgba(0, 0, 0, 1)">, i);
len</span>--<span style="color: rgba(0, 0, 0, 1)">;
heapify(arr, </span>0<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span> buildMaxHeap(arr) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 建立大顶堆</span>
len =<span style="color: rgba(0, 0, 0, 1)"> arr.length;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = Math.floor(len / 2); i >= 0; i--<span style="color: rgba(0, 0, 0, 1)">) {
heapify(arr, i);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span> heapify(arr, i) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 堆调整</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> left = 2 * i + 1<span style="color: rgba(0, 0, 0, 1)">,
right </span>= 2 * i + 2<span style="color: rgba(0, 0, 0, 1)">,
largest </span>=<span style="color: rgba(0, 0, 0, 1)"> i;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (left < len && arr[left] ><span style="color: rgba(0, 0, 0, 1)"> arr[largest]) {
largest </span>=<span style="color: rgba(0, 0, 0, 1)"> left;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (right < len && arr[right] ><span style="color: rgba(0, 0, 0, 1)"> arr[largest]) {
largest </span>=<span style="color: rgba(0, 0, 0, 1)"> right;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (largest !=<span style="color: rgba(0, 0, 0, 1)"> i) {
swap(arr, i, largest);
heapify(arr, largest);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> swap(arr, i, j) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> temp =<span style="color: rgba(0, 0, 0, 1)"> arr[i];
arr[i] </span>=<span style="color: rgba(0, 0, 0, 1)"> arr[j];
arr[j] </span>=<span style="color: rgba(0, 0, 0, 1)"> temp;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr;
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> arr = [2,6,5,3,8,4,10,23<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [2, 6, 5, 3, 8, 4, 10, 23]</span>
heapSort(arr);
console.log(arr); // [2, 3, 4, 5, 6, 8, 10, 23]
8.计数排序:
*概念:计数排序不是基于比较的排序算法,其核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。 作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。
计数排序是一个稳定的排序算法。当输入的元素是 n 个 0到 k 之间的整数时,时间复杂度是O(n+k),空间复杂度也是O(n+k),其排序速度快于任何比较排序算法。当k不是很大并且序列比较集中时,计数排序是一个很有效的排序算法。
*算法描述:
-
找出待排序的数组中最大和最小的元素;
-
统计数组中每个值为i的元素出现的次数,存入数组C的第i项;
-
对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加);
-
反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1。
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142910579.gif)
*算法实例:
function countingSort(arr, maxValue) {
var bucket = new Array(maxValue + 1),
sortedIndex = 0,
arrLen = arr.length,
bucketLen = maxValue + 1;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i < arrLen; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">bucket[arr[i]]) {
bucket[arr[i]] </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
}
bucket[arr[i]]</span>++<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> j = 0; j < bucketLen; j++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (bucket[j] > 0<span style="color: rgba(0, 0, 0, 1)">) {
arr[sortedIndex</span>++] =<span style="color: rgba(0, 0, 0, 1)"> j;
bucket[j]</span>--<span style="color: rgba(0, 0, 0, 1)">;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr;
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> arr = [4, 3, 3, 2, 3, 4, 5, 6, 3, 5, 6, 4, 6, 5, 2, 1, 2<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr);
countingSort(arr,</span>6<span style="color: rgba(0, 0, 0, 1)">);
console.log(arr);</span></pre></code-box>
9.桶排序:
*概念:桶排序是计数排序的升级版。它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定。
桶排序 (Bucket sort)的工作的原理:假设输入数据服从均匀分布,将数据分到有限数量的桶里,每个桶再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排)。
桶排序最好情况下使用线性时间O(n),桶排序的时间复杂度,取决与对各个桶之间数据进行排序的时间复杂度,因为其它部分的时间复杂度都为O(n)。很显然,桶划分的越小,各个桶之间的数据越少,排序所用的时间也会越少。但相应的空间消耗就会增大。
*算法描述:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517142953318.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIxMzk3ODE1,size_16,color_FFFFFF,t_70)
*算法实例:
function bucketSort(arr, bucketSize) {
if (arr.length === 0) {
return arr;
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span><span style="color: rgba(0, 0, 0, 1)"> i;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> minValue = arr[0<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">var</span> maxValue = arr[0<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 1; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr[i] <<span style="color: rgba(0, 0, 0, 1)"> minValue) {
minValue </span>= arr[i]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输入数据的最小值</span>
} <span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (arr[i] ><span style="color: rgba(0, 0, 0, 1)"> maxValue) {
maxValue </span>= arr[i]; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 输入数据的最大值</span>
}
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 桶的初始化</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> DEFAULT_BUCKET_SIZE = 5; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置桶的默认数量为5</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 桶的个数</span>
bucketSize = parseInt(bucketSize) > 0 ?<span style="color: rgba(0, 0, 0, 1)"> parseInt(bucketSize) : DEFAULT_BUCKET_SIZE;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分成 bucketSize 个桶,桶所占用的范围</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> bucketCount = Math.floor((maxValue - minValue) / bucketSize) + 1<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 初始化桶</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> buckets = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Array(bucketCount);
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i < buckets.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
buckets[i] </span>=<span style="color: rgba(0, 0, 0, 1)"> [];
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 利用映射函数将数据分配到各个桶中</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
buckets[Math.floor((arr[i] </span>- minValue) /<span style="color: rgba(0, 0, 0, 1)"> bucketSize)].push(arr[i]);
}
arr.length </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (i = 0; i < buckets.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
insertionSort(buckets[i]); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对每个桶进行排序,这里使用了插入排序</span>
<span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> j = 0; j < buckets[i].length; j++<span style="color: rgba(0, 0, 0, 1)">) {
arr.push(buckets[i][j]);
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr;
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> insertionSort(arr) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr == <span style="color: rgba(0, 0, 255, 1)">null</span> || arr.length < 2<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr;
}
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 1; i < arr.length; i++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">for</span> (let j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--<span style="color: rgba(0, 0, 0, 1)">) {
let temp </span>=<span style="color: rgba(0, 0, 0, 1)"> arr[j];
arr[j] </span>= arr[j + 1<span style="color: rgba(0, 0, 0, 1)">];
arr[j </span>+ 1] =<span style="color: rgba(0, 0, 0, 1)"> temp;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> arr;
}
}
</span><span style="color: rgba(0, 0, 255, 1)">var</span> arr = [8, 39, 400, 500, 3, 4, 20, 44, 440<span style="color: rgba(0, 0, 0, 1)">];
console.log(arr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [8, 39, 400, 500, 3, 4, 20, 44, 440]</span>
bucketSort(arr, 10<span style="color: rgba(0, 0, 0, 1)">);
console.log(arr); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> [3, 4, 8, 20, 39, 44, 400, 440, 500]</span></pre></code-box>
10.基数排序:
*概念:基数排序是按照低位先排序,然后收集;再按照高位排序,然后再收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,再按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。
基数排序基于分别排序,分别收集,所以是稳定的。但基数排序的性能比桶排序要略差,每一次关键字的桶分配都需要O(n)的时间复杂度,而且分配之后得到新的关键字序列又需要O(n)的时间复杂度。假如待排数据可以分为d个关键字,则基数排序的时间复杂度将是O(d*2n) ,当然d要远远小于n,因此基本上还是线性级别的。
基数排序的空间复杂度为O(n+k),其中k为桶的数量。一般来说n>>k,因此额外空间需要大概n个左右。
*算法描述:
*动画演示:
![](https://img-blog.csdnimg.cn/20190517143109965.gif)
*算法实例:
function radixSort(arr, maxDigit) {
var counter = [];
var mod = 10;
var dev = 1;
for (var i = 0; i < maxDigit; i++, dev *= 10, mod *= 10) {
// 把待排序的数组 arr 中的每一位整数,插入对应的容器
for (var j = 0; j < arr.length; j++) {
// 从个位开始,得到数组中每个数的每一位并保存在 bucket 变量中
// bucket 变量的值可能为 0 1 2 3 4 5 6 7 8 9
// 与之对应的 counter[bucket] 容器为 0 1 2 3 4 5 6 7 8 9
var bucket = parseInt((arr[j] % mod) / dev);
// 如果目前 bucket 变量的值对应的 counter[bucket] 容器还不存在(未初始化),则创建(初始化)一个新的空容器
if (counter[bucket] == null) {
counter[bucket] = [];
}
// 现在把这个 bucket 变量的值插入对应的 counter[bucket] 容器的尾部
counter[bucket].push(arr[j]);
}
// 把 counter[bucket] 容器里的数依次取出
var pos = 0;
for (var j = 0; j < counter.length; j++) {
var value = null;
if (counter[j] != null) {
while ((value = counter[j].shift()) != null) {
arr[pos++] = value;
}
}
}
}
return arr;
}
var arr = [4, 5, 3, 1, 7, 4, 3, 2, 0, 4, 3];
console.log(arr); // [4, 5, 3, 1, 7, 4, 3, 2, 0, 4, 3]
radixSort(arr, 3)
console.log(arr); // [0, 1, 2, 3, 3, 3, 4, 4, 4, 5, 7]