当前位置:网站首页>js快速排序
js快速排序
2022-06-12 17:58:00 【走出自闭的鸟儿】
快速排序
/** * 获取midValue的两种方式 * 使用splice,会修改原数组 * 使用slice,不会修改原数组 * O(n*logn) * 单独比较splice和slice,slice快很多 */
function quickSort(arr){
const length = arr.length
if(length === 0) return arr
const midIndex = Math.floor(length/2)
// splice修改原数组,返回被删除元素的数组
const midValue = arr.splice(midIndex,1)[0]
const left = []
const right = []
// 注意:这里不能直接用length,而是用arr.length,因为arr已经被splice修改
for(let i=0;i<arr.length;i++){
if(arr[i]<midValue){
// 小于放在左
left.push(arr[i])
}else{
//大于放在右
right.push(arr[i])
}
}
return quickSort(left).concat([midValue],quickSort(right))
}
const arr = [1,6,2,7,8,5,3]
console.log(quickSort(arr));
边栏推荐
- Leetcode 674 longest incrementing substring
- Are Huishang futures accounts reliable? Is the fund safe?
- 认识函数原创
- The server time zone value ‘� й ��� ʱ ��‘ is unrecognized or represents more than one time zone. ......
- Vant3+ts H5 pages are nested into apps to communicate with native apps
- Database SQL operation Basics
- Small program +app, a low-cost and active technology combination idea
- Original error interface
- Error record: illegalstateexception: optional int parameter 'XXXX' is
- vant3+ts 封装uploader上传图片组件
猜你喜欢

JDBC快速入门教程

Tensorflow prompts typeerror: unsupported operand type (s) for *: 'float' and 'nonetype‘

Vant3+ts H5 pages are nested into apps to communicate with native apps

Extreme Programming -- Practice of root cause analysis

Vant3 +ts packaged simple step advancer component

Overall flow chart of kernel interrupt

机器学习系列(3):Logistic回归

极限编程--根源分析实践

High speed layout guidelines incomplete
![[CSP]202012-2期末预测之最佳阈值](/img/40/9b59bd692bcfe05d16614cc6d55d1f.png)
[CSP]202012-2期末预测之最佳阈值
随机推荐
406. reconstruct the queue based on height
String s = null ; String s = new String(); String s = "; what is the difference between string s?
grpc-swift入门
C#简单介绍
Cesium抛物线方程
Second week of electric control learning
有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
ES7 does not use parent-child and nested relationships to implement one to many functions
A method of quickly reusing wechat login authorization in app
PHP implementation of infinite classification tree (recursion and Optimization)
566. 重塑矩阵
小程序+App,低成本获客及活跃的一种技术组合思路
Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
Detailed description of SQL cursor and example of internal recycling
低代码平台调研结果
Small program +app, a low-cost and active technology combination idea
Graphical data model for system design
Vant3+ts H5 pages are nested into apps to communicate with native apps
Overall flow chart of kernel interrupt
ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间