当前位置:网站首页>排序——QuickSort
排序——QuickSort
2022-07-24 04:38:00 【向往神秘的天堂】
快速排序
快速排序可以由Hoare法、挖坑法、前后指针法三种方法完成,其中Hoare法的核心思想为:选取数组最左值作为KEY,然后让数组最右边的标记点(记作end)先走(从右往左走)直到找到第一个小于KEY的数组,然后让让数组最左边的标记点(记作begin)往右走,直到找到第一个大于KEY的数字,然后将以上找到的两个数字进行交换,重复上述过程,直到两个标记点相遇(因为两个标记点走的步长为一,故标记点一定相遇)最后在相遇点,将KEY的值置于此处,新得数组从此处向左一定都小于KEY值,向右一定大于KEY值,然后以begin到KEY-1与KEY+1到end划分为两个区间分别进行递归,直begin>end为主递归结束,此时递归返回数组为有序数组,过程如下图所示

第一趟完成后的数字如下图所示:
6左边都小于6,右边都大于6,接着就是以6左边作为一个区间和6右边作为一个区间,分别重复上述过程,完成对数组的排序过程,Hoare法代码如下所示:
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
{
//不一定是最开始的十个数,有可能是中间的十个数,故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);
}
后续继续分享快速排序的其余两种方法。
边栏推荐
- Basic syntax of MySQL DDL and DML and DQL
- Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc
- 一次线上事故,我顿悟了异步的精髓
- How much do you know about thread pool and its application
- C language: selective sorting method
- Unable to delete the file prompt the solution that the file cannot be deleted because the specified file cannot be found
- Billiard simulator based on the integration of GL pipeline and ray tracing technology
- Particle Designer:粒子效果制作器,生成plist文件并在工程中正常使用
- LabVIEW master VI freeze pending
- ECB interface is also mdsemodet in essence
猜你喜欢

PMIX ERROR: ERROR in file gds_ds12_lock_pthread.c

C语言经典习题之编写一个程序,找出1000以内所有的完数。

Billiard simulator based on the integration of GL pipeline and ray tracing technology

Logback log framework technology in project development

OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication

LabVIEW主VI冻结挂起

IP second experiment mGRE OSPF

Division of training set, verification set and test set in link prediction (take randomlinksplit of pyg as an example)

Esp32 tutorial (I): vscode+platform and vscade+esp-idf

Journey of little black leetcode: 590. Post order traversal of n-ary tree
随机推荐
Airiot Q & A issue 5 | how to use low code business flow engine?
Will your NFT disappear? Dfinity provides the best solution for NFT storage
Little black leetcode journey: 100 same trees
Basic syntax of MySQL DDL and DML and DQL
仿今日头条实时新闻微信小程序项目源码
What is the real HTAP? (2) Challenge article
How long has it been since you changed your cell phone?
Xiaomi finance was officially launched today (May 11) with a free 10000 yuan experience fee attached to the official address
基于GL Pipeline与光线追踪技术的融合实现的台球模拟器
Can NFT pledge in addition to trading?
Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
智能合约:发布一种ERC20代币
Application scenarios and schemes of common mechanical equipment safety virtual simulation system
How to play the Microsoft twin tool twinsonot? Introduction to twin test tool twinornot
C语言:随机数的生成
Smart people's game improvement: Chapter 3 Lesson 3 example: the secret of prime
高频小信号谐振放大器设计-课程设计Multisim仿真
Engineer competency model and skill requirements
链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)
Shell syntax (1)