当前位置:网站首页>Advanced sorting - fast sorting
Advanced sorting - fast sorting
2022-04-23 03:15:00 【GeXueliu】
Quick line up
var sortArray = function(nums) {
// Quick line up
return quickStart(nums,0,nums.length-1);
}
function quickStart(nums,left,right) {
if(left<right) {
let index = partition(nums,left,right);
quickStart(nums,0,index);
quickStart(nums,index+1,right);
}
return nums;
}
// Partition : Speed pointer
function partition(array,left,right) {
let pvit = left; // Select the leftmost side as the reference value , Can be optimized ( The leftmost may always be a smaller number , Take this as the reference value , Cause the recursive tree to tilt to one side )
let index = left+1;
for(let i=index;i<=right;i++) { // i Pointer to a value less than the reference value , The previous elements are greater than the reference value .index Pointer to a value greater than the reference value
if(array[i]<array[pvit]) {
swap(array,index,i);
index++;
}
}
swap(array,pvit,index-1);
return index-1;
}
// Swap elements in an array
function swap(array,i,j) {
let temp = array[i];
array[i] = array[j];
array[j] = temp;
}
// Partition : Dig a hole
function partition2(array,left,right) {
let pivot = array[left]; // The left element acts as the first pit , It is also the reference value
while(left<right) {
while(left<right&&array[right]>=pivot) { // stay right In the process of moving ,>right Are greater than the reference value ,<left Are less than the reference value ,=left Greater than the reference value
right--;
}
if(left<right) array[left] = array[right];
while(left<right&&array[left]<=pivot) { // stay left In the process of moving ,<left Are less than the reference value ,>right Are greater than the reference value ,=right Less than reference value
left++;
}
if(left<right) array[right] = array[left];
}
array[left] = pivot; // The two elements meet , The meeting point is also the pit , At this time, the pit level is greater than the reference value , The pit level is lower than the reference value before , Fill in the reference value of the pit position
return left;
}
// Partition : Double pointer
function partition3(array,left,right) {
let pivot = array[left]; // The reference value is selected on the left
let low = left;
while(left<right) {
while(left<right&&array[right]>=pivot) { // The right pointer moves first ,i、j Meet on the left side of the dividing line ; Left pointer moves first ,i、j Meet on the right side of the dividing line
right--;
}
while(left<right&&array[left]<=pivot) {
left++;
}
if(left>=right) break;
swap(array,left,right); // i、j meet , That is, the array completes the partition , Just move the reference value to the dividing point
}
swap(array,low,left);
return left;
}
// When the array is an ordered sequence , Since the reference value is selected on the left , Causes the recursive tree to tilt , The sorting algorithm degenerates into O(N^2). Optimize : Randomly select a number as the reference value
// Optimize :
function quickStart(nums,left,right) {
if(left<right) {
let pivot = Math.floor(Math.random()*(right-left+1))+left; // The random value is used as the reference value
swap(nums,left,pivot) // Just change the random value to the far left
let index = partition(nums,left,right);
quickStart(nums,0,index);
quickStart(nums,index+1,right);
}
return nums;
}
1. Further optimization : When the random value is the minimum or maximum value every time, it will also cause the recursive tree to tilt , At this time, the three number limit is adopted , The recursive tree can be completely avoided from being too tilted . see leetcode Official website .
2. The above are sorted from small to large , So how to modify the code in reverse order ?
版权声明
本文为[GeXueliu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627323228.html
边栏推荐
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
- General test technology [II] test method
- 软件测试相关知识~
- 【无标题】
- How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
- Mysql database, inconsistent index character set, slow SQL query, interface timeout
- OLED multi-level menu record
- Top 9 task management system in 2022
- Using stack to solve the problem of "mini parser"
- 2022t elevator repair test simulation 100 questions and online simulation test
猜你喜欢
![[MySQL] left function | right function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left function | right function
![[MySQL] left Function | Right Function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left Function | Right Function

Recursion - outputs continuously increasing numbers

Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)

Experiment 6 input / output stream

Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).

2022t elevator repair test simulation 100 questions and online simulation test

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

This new feature of C 11, I would like to call it the strongest!

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
随机推荐
Mysql database, inconsistent index character set, slow SQL query, interface timeout
先中二叉建树
C read / write binary file
poi根据数据创建导出excel
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Queue storage and circular queue
Comprehensive calculation of employee information
[authentication / authorization] customize an authentication handler
Xamarin effect Chapter 22 recording effect
Fight leetcode again (290. Word law)
手机连接电脑后,QT的QDIR怎么读取手机文件路径
通过 zxing 生成二维码
为什么BI对企业这么重要?
. net core current limiting control - aspnetcoreratelimit
2022年度Top9的任务管理系统
[MySQL] left function | right function
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
First in the binary tree
General test technology [II] test method
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】