当前位置:网站首页>Sort - quicksort
Sort - quicksort
2022-07-24 04:40:00 【Yearn for mysterious paradise】
Quick sort
Quick sort can be done by Hoare Law 、 Excavation method 、 The front and back pointer method is completed in three ways , among Hoare The core idea of law is : Select the leftmost value of the array as KEY, Then let the rightmost marker point of the array ( Write it down as end) Go ahead ( Go from right to left ) Until you find the first one less than KEY Array of , Then let the leftmost marker point of the array ( Write it down as begin) Go to the right , Until we find the first one greater than KEY The number of , Then exchange the two numbers found above , Repeat the process , Until the two marked points meet ( Because the step length of two marked points is one , So the marker points must meet ) Finally, at the meeting point , take KEY The value of is placed here , The new array must be smaller than... From here to the left KEY value , To the right must be greater than KEY value , And then to begin To KEY-1 And KEY+1 To end It is divided into two intervals for recursion , straight begin>end Main recursion ends , At this time, the recursive returned array is an ordered array , The process is shown in the figure below 

The number after the first trip is shown in the figure below :
6 The left is smaller than 6, The right side is larger than 6, Then, with 6 The left is an interval and 6 The right is an interval , Repeat the above process respectively , Complete the sorting process of the array ,Hoare The method code is as follows :
void Swap(int* p, int* q)
{
int tmp = *p;
*p = *q;
*q = tmp;
}
int Part1Sort(int* a, int begin, int end)
{
int Mid = GetMidIndex(a, begin, end);
Swap(&a[Mid], &a[begin]);
int left = begin;
int right = end;
int keyi = left;
while (left < right)
{
while (a[right] >= a[keyi]&&left<right)
{
--right;
}
while (a[left] <= a[keyi]&&left<right)
{
++left;
}
Swap(&a[left], &a[right]);
}
Swap(&a[right],&a[keyi]);
keyi = left;
return keyi;
}
void QuickSort(int *a,int begin,int end)
{
if (begin >= end)
{
return;
}
if (end - begin > 10)
{
int keyi = Part1Sort(a,begin,end);
//int keyi = Part2Sort(a, begin, end);
//int keyi = Part3Sort(a, begin, end);
QuickSort(a, begin, keyi - 1);
QuickSort(a, keyi + 1, end);
}
else
{
// Not necessarily the first ten numbers , It could be the middle ten numbers , so a+begin
InserSort(a+begin,end-begin+1);
}
}
void TestQuickSort()
{
int a[] = {
1,2,9,4,6,5,3,7,8 ,-1,-5,15,20,25,100,111};
int n = sizeof(a) / sizeof(int);
QuickSort(a,0,n-1);
PrintArray(a, n);
}
We will continue to share the other two methods of quick sorting .
边栏推荐
- Xiaomi finance was officially launched today (May 11) with a free 10000 yuan experience fee attached to the official address
- The software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
- 格式问题处理
- Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
- Nautilus 3.19.2 adds momentum to Gnome
- 高频小信号谐振放大器设计-课程设计Multisim仿真
- 工程师能力模型与技能要求
- Black one-stop operation and maintenance housekeeper 10 records ro
- Introduction to the application fields and functions of bank virtual human technology
- To 3mm; Provide safe and stable product execution according to the sender IID
猜你喜欢

What is the real HTAP? (2) Challenge article
![[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion](/img/24/16cfb44dde056f4b91cdb1de2e9566.png)
[cornerstone of high concurrency] multithreading, daemon thread, thread safety, thread synchronization, mutual exclusion

LabVIEW主VI冻结挂起

Chery arizer 8 products are powerful, and "all excellent" is not for nothing

Design of two power dividers and directional couplers for basic experiment of microwave technology

Gau and ppm for image semantic segmentation
![Graduation thesis on enterprise production line improvement [Flexsim simulation example]](/img/83/381ef1566d5a863b709f504b46e169.png)
Graduation thesis on enterprise production line improvement [Flexsim simulation example]

链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)

Array force buckle (continuously updated)

一次线上事故,我顿悟了异步的精髓
随机推荐
-Bash: wget: command not found
An online accident, I suddenly realized the essence of asynchrony
How long has it been since you changed your cell phone?
Black one-stop operation and maintenance housekeeper 10 records ro
【2023芯动科技提前批笔试题】~ 题目及参考答案
P一个配置文件期间将SDA松集成。支但事实上
微波技术基础实验二 功分器与定向耦合器设计
00cm的非,与业务方确预上线一次,把所为有更好的泛
[2023 core technology approval test questions in advance] ~ questions and reference answers
What does the red five pointed star in the lower right corner of sina Weibo avatar mean? How to become a master of sina Weibo?
There is not enough space on the disk to complete this operation when partitioning the computer
The software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
Division of training set, verification set and test set in link prediction (take randomlinksplit of pyg as an example)
How is it that desktop icons can't be dragged? Introduction to the solution to the phenomenon that desktop file icons can't be dragged
Little black leetcode journey: 100 same trees
Traversal of binary tree
C language: generation of random numbers
Excel cell formula - realize Ackerman function calculation
Er system, in Lin reply bit, count, successfully open r com change
Graduation thesis on enterprise production line improvement [Flexsim simulation example]