当前位置:网站首页>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 .
边栏推荐
- Export function called separately
- [essay] goodbye to Internet Explorer, and the mark of an era will disappear
- C主机对IIC从来分别设置每足够的话,可下几位
- 最大公约数
- The second anniversary of opengauss' open source, cracking the pain point of database ecology
- Format problem handling
- Question 146: LRU cache
- Graduation thesis on enterprise production line improvement [Flexsim simulation example]
- Middle aged crisis, workplace dad who dare not leave, how to face life's hesitation
- Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind
猜你喜欢

The judges of C language classic exercises score the highest and lowest to get an average score

Post SQL era: edgedb 2.0 Release Notice

Privacy protection federal learning framework supporting most irregular users
![Graduation thesis on enterprise production line improvement [Flexsim simulation example]](/img/83/381ef1566d5a863b709f504b46e169.png)
Graduation thesis on enterprise production line improvement [Flexsim simulation example]

pycharm 调试功能介绍与使用

64 attention mechanism 10 chapters

What is the real HTAP? (2) Challenge article

Smart contract: release an erc20 token

微波技术基础实验二 功分器与定向耦合器设计

Chery arizer 8 products are powerful, and "all excellent" is not for nothing
随机推荐
How to register and apply for free for Apple Developer account in order to enjoy the upgrade experience at the first time
C主机对IIC从来分别设置每足够的话,可下几位
dispatch_ Once's Secret
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为Gnome增添动力
IP second experiment mGRE OSPF
工程师能力模型与技能要求
Basic learning notes of C language
Journey of little black leetcode: 590. Post order traversal of n-ary tree
到3mm;提供安全稳定的产品作的执行据发出方iid
Format problem handling
How much do you know about thread pool and its application
What is the general family of programmers working in first tier cities?
Little black leetcode journey: 100 same trees
高频小信号谐振放大器设计-课程设计Multisim仿真
Introduction and use of pycharm debugging function
Face algorithms
Esp32:arduino tutorial summary
Why can't I hide folders? Solutions to the hidden folders on the computer that can still be seen
To -.---