当前位置:网站首页>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
边栏推荐
- 通过 zxing 生成二维码
- be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
- 一文了解全面静态代码分析
- Improvement of ref and struct in C 11
- OLED multi-level menu record
- 软件测试相关知识~
- Recursion - outputs continuously increasing numbers
- A set of combination boxing to create an idea eye protection scheme
- 12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
- Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
猜你喜欢
关于idea调试模式下启动特别慢的优化
Drawing polygons with < polygon / > circular array in SVG tag
Xamarin effect Chapter 22 recording effect
The most easy to understand dependency injection and control inversion
C WPF UI framework mahapps switching theme
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
软件测试相关知识~
OLED multi-level menu record
[Mysql] LEFT函數 | RIGHT函數
LoadRunner - performance testing tool
随机推荐
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
2022T电梯修理考试模拟100题及在线模拟考试
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Eight elder brothers chronicle [4]
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
Use of ADB command [1]
EasyUI's combobox implements three-level query
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
LoadRunner - performance testing tool
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Experiment 5 components and event handling
编码电机PID调试(速度环|位置环|跟随)
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
yes. Net future
Mysql database, inconsistent index character set, slow SQL query, interface timeout
Xamarin effect Chapter 21 expandable floating operation button in GIS
2022山东省安全员C证上岗证题库及在线模拟考试