当前位置:网站首页>JS quick sort
JS quick sort
2022-06-12 18:02:00 【Out of the autistic bird】
Quick sort
/** * obtain midValue Two ways * Use splice, The original array will be modified * Use slice, The original array will not be modified * O(n*logn) * Compare them separately splice and slice,slice Much faster */
function quickSort(arr){
const length = arr.length
if(length === 0) return arr
const midIndex = Math.floor(length/2)
// splice Modify the original array , Returns an array of deleted elements
const midValue = arr.splice(midIndex,1)[0]
const left = []
const right = []
// Be careful : You can't use it directly here length, It's about using arr.length, because arr Has been splice modify
for(let i=0;i<arr.length;i++){
if(arr[i]<midValue){
// Less than left
left.push(arr[i])
}else{
// Larger than on the right
right.push(arr[i])
}
}
return quickSort(left).concat([midValue],quickSort(right))
}
const arr = [1,6,2,7,8,5,3]
console.log(quickSort(arr));
边栏推荐
- vant3 +ts 封装简易step进步器组件
- JS dichotomy
- USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率
- LCD parameter interpretation and calculation
- Still using Microsoft office, 3 fairy software, are you sure you don't want to try?
- Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
- Leetcode491 increasing subsequence
- leetcode491 递增子序列
- C brief introduction
- leetcode 300. 最长递增子序列
猜你喜欢

企业架构的第一性原理

PHP implementation of infinite classification tree (recursion and Optimization)

Nixos 22.05 installation process record

Make good use of IDE, speed up R & D efficiency by 100%

用好IDE,研发效能提速100%

LCD parameter interpretation and calculation

First principles of enterprise architecture

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

USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率

js求斐波那契数列
随机推荐
字节飞书人力资源套件三面
High-Speed Layout Guidelines 未完...
有源差分晶振原理图以及LV-PECL、LVDS、HCSL区别
Random talk about redis source code 90
Section qemu+gdb
The server time zone value ‘�й���ʱ��‘ is unrecognized or represents more than one time zone. ......
Leetcode491 increasing subsequence
Common dependencies of SSM
一种好用、易上手的小程序IDE
vant3+ts 封装uploader上传图片组件
USB to serial port - maximum peak serial port baud rate vs maximum continuous communication baud rate
leetcode 647. Palindrome substring
Vulnhub[DC3]
TypeScript类型声明文件(三)
es7不使用父子和嵌套关系来实现一对多功能
ESP-IDF 添加自己的组件
Schedule update | 2022 Microsoft and Intel hacker song competition is in hot registration
赛程更新| 2022微软与英特尔黑客松大赛火热报名中
Detailed description of SQL cursor and example of internal recycling
Eve-ng installation (network device simulator)