You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionquickSort2(A){if(A.length<2)returnAletpivotIndex=A.length>>1letpivotValue=A.splice(pivotIndex,1)[0]letleft=[],right=[]for(leti=0;i<A.length;i++){(A[i]>pivotValue ? right : left).push(A[i])}returnquickSort2(left).concat(pivotValue,quickSort2(right))}
桶的数量 N <= Array.length, 分成 N个桶, 分桶是根据 值所在的区间来分, 比如d = 最大值-最小值, value - 最小值 / d 就是桶的范围比 可求出对应的桶的索引值
在进行每个桶的排序, 可以是桶自己的排序 也可以其他排序
说明
桶排序弥补了 计数排序的一些缺陷, 数值必须是整数;
代码实现
functionbucketSort(A,bucketLen=A.length){constmax=Math.max.apply(null,A)constmin=Math.min.apply(null,A)constd=max-minconstbucketList=Array.apply(null,{length: bucketLen}).map(_=>[])for(leti=0;i<A.length;i++){// 索引结果: idx = len * d / s letidx=parseInt((A[i]-min)*(bucketLen-1)/d)bucketList[idx].push(A[i])}for(leti=0;i<bucketLen;i++){bucketList[i].sort((a,b)=>a-b)}returnbucketList.reduce((l,p)=>(l=l.concat(p)),[])}
堆排序
后续补充
归并排序
后续补充
The text was updated successfully, but these errors were encountered:
整理一下之前看《漫画算法》上的一些基础排序算法。部分资料引用wiki自己做过回顾,已经掌握好排序的童鞋,可以忽略。
排序算法
排序算法常见要求:
快速排序 o(n*logn)
具体实现
额外空间实现
这种空间复杂度高,代码简洁。
原地排序实现
冒泡排序 o(n*n)
分为有序区和无序区, 想象就跟水泡一下,沉的在下面 (有序区), 轻的在上面 (无序区)..
计数排序 o(n+k)
计数排序主要利用了数组索引自带排序特性
注: 最大值和最小值差太大, 会造成创建的数组过大, 内存过大;
当数组值不是整数 的时候 不适合用这种方法
代码实现
桶排序
样例数据 [1,2,4,100,50]
原理
说明
代码实现
堆排序
后续补充
归并排序
后续补充
The text was updated successfully, but these errors were encountered: