当前位置:网站首页>排序——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);
}
后续继续分享快速排序的其余两种方法。
边栏推荐
- 黑色的的一站式运维管家 10条记录RO
- Can NFT pledge in addition to trading?
- How to make yourself look young in how old robot? How old do I look? Younger method skills
- Basic learning notes of C language
- Smart people's game improvement: Chapter 3 Lesson 3 example: the secret of prime
- What are the 10 live demos showing? It's worth watching again whether you've seen it or not
- What if the computer can't take screenshots? The solution to the problem that the shortcut screen capture key of the computer cannot be used
- C语言经典习题之猴子吃桃问题
- 数组力扣(持续更新)
- Imitate today's headlines real-time news wechat applet project source code
猜你喜欢

Forward proxy, reverse proxy and XFF

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

基于C语言设计的一个医院叫号系统

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

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

Uniapp learning

Design of high frequency small signal resonant amplifier course design Multisim Simulation

64 attention mechanism 10 chapters

Live broadcast preview | practice sharing of opengauss' autonomous operation and maintenance platform dbmind

Journey of little black leetcode: 341. Flattening nested list iterator
随机推荐
《论文复现》BiDAF代码实现过程(3)模型建立
Nautilus 3.19.2为Gnome增添动力
The judges of C language classic exercises score the highest and lowest to get an average score
The software cannot be uninstalled. Please wait for the current program to complete the uninstallation or change the solution
智能合约:发布一种ERC20代币
What if the computer time is often inaccurate? Set up tutorials to automatically update and proofread computer time
Chery arizer 8 products are powerful, and "all excellent" is not for nothing
Little black gnawing leetcode:589. Preorder traversal of n-ary tree
Solution to the prompt of online account opening and transfer in stock speculation that the depository and transfer services are not activated (China Merchants Bank)
C language: selective sorting method
How long has it been since you changed your cell phone?
LabVIEW主VI冻结挂起
Which is better, Xiaomi current treasure or yu'e Bao? Detailed comparison and introduction of the differences between Xiaomi current Bao and Alibaba yu'e Bao
基于C语言设计的一个医院叫号系统
Traversal of binary tree
Airiot Q & A issue 5 | how to use low code business flow engine?
项目普遍格式问题 src下添加 .eslinctrc.js
C语言经典习题之评委打分去掉最高最低求平均分
力扣146题:LRU缓存
致-.-- -..- -