当前位置:网站首页>JS数组方法 sort() 排序规则解析
JS数组方法 sort() 排序规则解析
2022-07-24 18:08:00 【无围之解】
我们用一个数组来测试sort()函数的排序方法,这里以升序为例。
打印参数 m、n 可以知道这个函数中究竟传入了什么值,借此就可以判断出来用的什么方法进行比较的:
var a = [3,1,5,4,8,9,32,19,6,21,77,64,15];
a.sort(function (m,n){
console.log( "m="+m+","+"n="+n )
return m-n;
})
console.log( a )
来看一下打印结果,
整个查找元素的过程,用到的方法其实就是折半查找,给大家画个图,带大家一探究竟:
这里我们先跳过前面7个元素的排序过程,因为整个比较过程所用到的方法是相同的。
我们发现:
首先会选择19前面的7个数字中,中间的那个数字(中间数字下标的计算方法:首尾下标的和的/2,将结果向上取整,例如3/2结果是1.5,向上取整得到的就是2),和其进行比较,
当大于这个数字时,就和其后面的元素再进行折半查找,
当小于这个数字时,就和其前面的元素进行折半查找,
直到找到一个位置,满足大于其前面的数字,小于其后面的数字。
小于首元素时,就将这个数字放数组的头部,大于尾元素时,就将这个数字放数组的尾部。
总结:JS的 sort() 排序,用到的是折半查找的方法。
注意,不能以 0 作为参考的分界线,也就是说,不是说 m - n > 0 就是降序,m - n < 0 就是升序,因为我们根本不能保证每次传入的两个参数谁大谁小。通过上面的图可以看到,m 表示新加入的要进行排序的元素,而 n 表示折半查找到的元素,这两者的大小关系不不是确定的。
边栏推荐
猜你喜欢
![[OBS] dependency Library: x264 vs Build](/img/24/4caea5dc6ff092a3161f43b6026d25.png)
[OBS] dependency Library: x264 vs Build

C语言实现静态版本的通讯录

In depth analysis of the famous Alibaba cloud log4j vulnerability

Use of jumpserver

Shardingsphere database read / write separation

继承与派生

The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience

0630~ professional quality course

Emerging potential of interactive virtual reality technology in drug discovery
Link editing tips of solo blog posts illegal links
随机推荐
How to read "STL source code analysis"?
new也可以创建对象,为什么需要工厂模式?
Install jumpserver
Get the original data API on 1688app
Learn redisson from scratch ------- topics (subscription and distribution)
阿里巴巴/166获得店铺的所有商品 API使用说明
Codeforces Round #794 (Div. 2)(A.B.C)
还在用命令行看日志?快用Kibana吧,可视化日志分析YYDS!
0630~职业素养课
[network security] analysis vulnerability of website Middleware
安装JumpServer
2022最新短视频去水印解析API接口分享
Use of jumpserver
0613 ~ self study
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Inheritance and Derive
05mysql lock analysis
C language custom types - Enumeration
Flink operation Hudi data table
213. 打家劫舍 II-动态规划