当前位置:网站首页>Internal implementation principle of sort
Internal implementation principle of sort
2022-07-21 19:40:00 【Float544】
sort Basic use of the method :
arr.sort([compareFunction])
compareFunction A function used to specify a certain order , If you don't write . The element is converted into a string according to unicode Code to sort .
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
const arr1 = [1,23,43,34,5,4,12098];
arr1.sort();
console.log(arr1);
You can see , String sorting is no problem . But the numbers are not sorted correctly , Because after the number is converted into a string, the first element of the string unicode Determines the ordering of elements .
So you need to specify compareFunction.
a Value range nums[1] – nums[nums.length-1]
b Value range nums[0] – nums[nums.length-2]
If compareFunction(a, b) Less than 0, that a,b Location switching ;
If compareFunction(a, b) be equal to 0,a and b The relative position of ;
If compareFunction(a, b) Greater than 0,a and b The relative position of .
sort Sorting analysis
If the number of elements to sort is n When , Then there will be the following situations :
When n<=10 when , Use insert sort ;
When n>10 when , Use a quick sort ;
Theoretically , The time complexity of quick sorting is O(nlogn), The insertion order is O(n2)
actually , When the amount of data is large , Quick sorting has greater advantages . But when the amount of data is small , Insert sort performance will exceed quick sort .
边栏推荐
- 载二氢丹参酮Ⅰ白蛋白纳米粒/去甲斑蝥素白蛋白纳米粒/伏立康唑白蛋白纳米粒的制备研究
- 读书笔记:《次第花开》
- Model模态框点击其他除了模态框区域不消失
- Sorting out of high-frequency interview questions, answers and knowledge context of Android, a major Internet company in 2022
- 动态内存管理2之柔性数组
- Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
- Verilog语法基础HDL Bits训练 03
- armv8 DVFS
- MySQL进阶(B)
- 电磁场与电磁波实验三 熟悉Mathematica软件在电磁场领域的应用
猜你喜欢
笔试强训第18天
C language learning
Poste technique | a40i les trois problèmes de logiciel de carte réseau les plus courants, analysez - les un par un pour vous
受众分析与卸载分析全面升级,HMS Core分析服务6.6.0版本上新
List container series operations (detailed)
Basic knowledge of trigger (I)
MySQL optimization summary I
使用JPofiler工具分析OOM原因
Verilog grammar basics HDL bits training 03
蚓激酶白蛋白纳米粒/红细胞膜定向包裹血红蛋白-白蛋白纳米粒的研究制备
随机推荐
蚓激酶白蛋白纳米粒/红细胞膜定向包裹血红蛋白-白蛋白纳米粒的研究制备
Software testing interview question: what do you think are the advantages of testing?
蓝灯绿灯按时明灭,编程古鲁的密语
C语言学习
解决報錯:Uncaught TypeError: Cannot read properties of undefined (reading ‘install‘)
思源同步问题:cloud object not found v2.1.0
Use jpofiler tool to analyze the cause of OOM
Deep learning - (4) vgg16 image classification
Recommended collection | practical operation, data console selection example
Kubernetes technology and Architecture (V)
45. Record the training process of orienmask and the process of deploying Yunshi technology depth camera
Classic examples of C language: 21-30 examples: insertion sort, Hill sort 1, quick sort, Hill sort 2, recursion, completion, Fibonacci sequence, common divisor and common multiple, judging the number
Liunx kills processes with the same name in batches
Deep learning - (5) class of data imbalance_ weight
ASTM F 814 test method for specific optical density of smoke produced by solid materials for aerospace equipment
紫杉醇联合2-甲氧基雌二醇的白蛋白纳米粒/荜茇酰胺白蛋白纳米粒的研究制备
MySQL (2)
Software testing interview question: talk about your understanding of the two strategies of top-down integration and bottom-up integration in integration testing, and talk about their respective advan
电磁场与电磁波实验三 熟悉Mathematica软件在电磁场领域的应用
Software test interview question: bug management tool tracking process (using bugzilla as an example)