当前位置:网站首页>Three methods of quick sorting
Three methods of quick sorting
2022-06-11 21:57:00 【Code loving students】
Catalog
3. Front and back pointer method
1. hoare Law
Quick sort is Hoare On 1962 A binary tree structure of the exchange sort method , Let's first introduce the method he created .
Methods and steps :
1. Select the leftmost part of the array ( Far right ) As a benchmark key
2. From the right ( From the left ) Start screening , On the right ( From the left ) Select less than key Number of numbers
3. Then from the left ( From the right ) Start selecting greater than key Number of numbers
4. Exchange the two
5. Repeat the above cycle , Until the left and right meet
There is a difficulty here : Why do we choose the left as the benchmark? We should start from the right ?
Because starting from the right can ensure that the number of the last meeting place is less than key Of , This will satisfy the final result : The left is smaller than key, The right side is larger than key
The code implementation is as follows :
void swap(int*a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
//hoare edition
int Partition2(int*a,int left,int right){
int key=left; // From the far left
while(left<right){
// Start from the right
while(left<right&&a[right]>=a[key]){// Find the one on the right that is less than a[key] Value
right--;
}
while(left<right&&a[left]<=a[key]){ // Find the one on the left a[key] Value
left++;
}
swap(a,left,right); // Exchange when you find it
}
swap(a,left,key); // Finally, the reference number and the meeting point are exchanged , Return to the meeting point
return left;
}
void QuickSort(int*a,int left,int right){
if(right<=left){ // There is only one element in the array , Or the array does not exist
return;
}
// After a single sequence pivot Is the final location
int pivot=Partition3(a,left,right);
// Using the idea of partition
QuickSort(a,left,pivot-1);
QuickSort(a,pivot+1,right);
}2. Excavation method
Pit digging method is an updated version of quick sorting .
Implementation steps :
1. Based on the leftmost number key, And keep its data .
2. It can be seen that the position of the reference number is empty , Then start from the far right and look for less than key The number of is placed in this pit .
3. When you find the right number to place the left pit , Then there is one more pit on the right , Then start from the left and find one larger than key Put the number in this pit .
4. Repeat the above steps , Until the left and right finally meet .
Code implementation :
void swap(int*a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int Partition(int*a,int left,int right){
// Excavation method
int key=a[left];
// Dig a hole
int pit=left; // Start selecting the pit position left
while(left<right){
while(left<right&&a[right]>=key){ // Find less than key Number of numbers
right--;
}
if(left<right){
a[pit]=a[right];// Fill in the pit
pit=right; // The pit becomes right Location
}
while(left<right&&a[left]<=key){ // Find greater than key Number of numbers
left++;
}
if(left<right){ // Empathy
a[pit]=a[left];
pit=left;
}
}
a[pit]=key; // The last pit is left==right The location of
return pit;
}Local variables are used pit Make the code more readable , What is the compact version ?
// Quick sort
int Partition(int*a,int left,int right){
// Excavation method
int key=a[left];
while(left<right){
while(left<right&&a[right]>=key){
right--;
}
if(left<right){
a[left]=a[right]
}
while(left<right&&a[left]<=key){
left++;
}
if(left<right){
a[right]=a[left]
}
}
a[left]=key;
return left;
}It's not here pit Variable , The code is more concise .
3. Front and back pointer method
It is the same as the excavation method and is also an updated version .
Implementation steps :
1. Use the pointer prev Point to the first position , The pointer cur Point to the next position .
2. When cur The data referred to is not less than key when ,cur Pointer backward
3. When cur The data referred to is less than key,prev The pointer moves back and coincides with cur Pointer for data exchange , then cur Move backward .
4. Repeat the above operation , until cur Out of bounds .
The code implementation is as follows :
void swap(int*a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
int Partition3(int*a,int left,int right){
int key=a[left];// Benchmark number
int prev=left;// Point to the first
int cur=1+left;// Point to the next position
int temp=0;
while(cur<=right){
// If prev Next door neighbor cur, Exchange is meaningless
if(a[cur]<key&&a[++prev]!=a[cur]){
swap(a,cur,prev);
}
cur++; //cur Move backward
}
swap(a,left,prev);// In exchange for prev and left The location of
return prev;// return prev
}Why to be cur Less than key And make sure that prev+1!=cur Just exchange ?
Because when prev Next door neighbor cur when , It indicates that there is no greater than... Between the two pointers key The data of , No need for self exchange
When prev+1!=cur when , It indicates that there is more than between the two pointers key The data of , When prev+1 Found greater than key Number of numbers , Follow cur Refers to less than key The exchange of the number of is less than key On the left , Greater than key It's on the right .
until cur Greater than right, Then it indicates that there is no less than key Number of numbers , Will prev And left swapping , complete Less than key On the left , Greater than key It's on the right .
边栏推荐
- Leetcode-76- minimum covering substring
- 206. reverse linked list
- RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation
- Add anti debugging function to game or code (application level)
- 206.反转链表
- 【学术相关】申请审核制下,到双一流大学读博的难度有多大?
- 类和对象(1)
- 继承的所有特征
- How to realize double speed playback and fast forward for restricted ckplayer players
- 科普 | NFT的类型有哪些(上)
猜你喜欢

《物联网开发实战》18 场景联动:智能电灯如何感知光线?(上)(学习笔记)

crontab中定时执行shell脚本

Latex combat notes 3- macro package and control commands

Usage of esp32c3 Arduino Library

Classes and objects (4)

Endnotex9 introduction and basic tutorial instructions

科普 | NFT的类型有哪些(上)

Leetcode-110-balanced binary tree

「大模型」之所短,「知识图谱」之所长

行而不辍,未来可期|云扩科技入选上海市专精特新企业
随机推荐
How to use the transaction code sat to find the name trial version of the background storage database table corresponding to a sapgui screen field
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.2
Leetcode-155-minimum stack
超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
win10字体模糊怎么调节
揭秘爆款的小程序,为何一黑到底
Parker plunger pump pv180r1k1t1nmmc
Look for leap years and see how many leap years I have had since I was born (I have had five)
【学术相关】申请审核制下,到双一流大学读博的难度有多大?
动态内存管理(1)
LaTex实战笔记 3-宏包与控制命令
高考结束,人生才刚刚开始,10年职场老鸟给的建议
Flutter series: detailed explanation of container layout commonly used in flutter
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4
Huawei equipment configuration h-vpn
常用分页方法总结
[v2.1] automatic update system based on motion step API (repair bug, increase completion display, support disconnection reconnection and data compensation)
In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
二分查找 - 学习
每日一题 -- 验证回文串