当前位置:网站首页>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));
边栏推荐
- Gossip about the 88 of redis source code
- Sqlserver common statements and functions
- MySQL学习笔记
- Research results of low code platform
- JS中的数组(含leetcode例题)<持续更新~>
- 轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通
- The server time zone value ‘� й ��� ʱ ��‘ is unrecognized or represents more than one time zone. ......
- SSM集成FreeMarker以及常用语法
- leetcode 647. Palindrome substring
- Vant3+ts H5 pages are nested into apps to communicate with native apps
猜你喜欢
随机推荐
Small program +app, a low-cost and active technology combination idea
使用MongoDB官方go库操作MongoDB原创
Second week of electric control learning
SSM integrates FreeMarker and common syntax
Extreme Programming -- Practice of root cause analysis
Authorization in Golang ProjectUseing Casbin
Leetcode 674 longest incrementing substring
Array sorts in the specified order
C operation database added business data value content case school table
重构--梳理并分解继承体系
轻量、便捷的小程序转App技术方案,实现与微信/流量App互联互通
小程序+App,低成本获客及活跃的一种技术组合思路
ESP32-C3 ESP-IDF 配置smartconfig 和 sntp 获取网络时间
Schematic diagram of active differential crystal oscillator and differences among lv-pecl, LVDS and HCSL
When openharmony meets openeuler
在同花顺开户证券安全吗
Kali2022 installing Armitage
channel原创
leetcode491 递增子序列
Graphical data model for system design









