当前位置:网站首页>八大排序之快速排序
八大排序之快速排序
2022-08-05 05:29:00 【又秃又弱】
思想:
从后往前找比基准小的数字,找到往前挪
从前往后找比基准大的数字,找到往后挪
思路演绎:
原始数据:9 3 12 6 7 10 5 8 21 4
temp:=9; //temp为基准,存放第一个数据
3 12 6 7 10 5 8 21 4
low high
第一趟:9>3,从后面High位置开始找比9小的数字,放在low位置
4 3 12 6 7 10 5 8 21
low high
high位置空出来,因此从前往后找比9大的数字放在High位置,low++
4 3 6 7 10 5 8 21 12
low high
low位置空出来,从High位置开始查找比9小的数字,high--
4 3 8 6 7 10 5 21
low high
high位置空出来,因此从前往后找比9大的数字放在High位置,low++
4 3 8 6 7 5 10 21
low high
low位置空出来,因此从后往前找比9小的数字放在low位置,high--
4 3 8 6 7 5 10 21
low high
此时,high=low,将temp的值放入该位置
4 3 8 6 7 5 9 10 21
上述为快速排序的一次划分,此时基准temp(9)抵达它应在位置。
为达到数据完整排序的目的,需在一次划分的基础上,返回此时low的位置,然后进行左右分别递归调用,从而实现排序。
空间复杂度:O(logn) //即递归的次数,每次都是/2。不是O(1)
时间复杂度:O(nlog2n) //一次划分是O(n),递归调用是O(nlog2n),可参照下面代码辅助理解
完全有序时,时间复杂度O(n^2) //举例为从小到大的数据,每个数据均需从后往前遍历一遍找比它小的数据,最后都是在low==high位置停止遍历。也可以从递归角度理解,每次切割数据时都不是/2,而是约等于切割1:n。
稳定性:不稳定
特点:越有序越慢,越无序越快
缺点:越有序越慢,空间复杂度大,不稳定。之所以叫快速排序,是从平均性能而言,快排最佳。
当数据趋于基本有序时,快速排序会退化为选择排序。
代码:
//一次划分
int Partition(int* arr, int low, int high)//int返回下标
{
int temp = arr[low];
while (low < high)
{
//从后往前找比基准temp小的数字
while (low<high && arr[high]>temp)
{
high--;
}
//找到比基准temp小的数字后,将赋值到low位置
if (low < high)
{
arr[low] = arr[high];
}
//从前往后找比基准temp大的数字
while (low < high && arr[low] <= temp)
{
low++;
}
//找到比基准temp大的数字后,将赋值到High位置
if (low < high)
{
arr[high] = arr[low];
}
}
arr[low] = temp;
return low;
}
//快速排序,递归实现
void Quick(int* arr, int low, int high)
{
int par= Partition(arr, low, high);//par=一次归并后low的位置
if (low < par - 1)//左边数据超过一个
{
Quick(arr, low, par - 1);//左递归
}
if (par + 1 < high)//处理右边
{
Quick(arr, par + 1,high);//右递归
}
}
//因为该函数参数的问题,无法直接递归调用,因此增加Quick函数
void QuickSort(int* arr, int len)
{
Quick(arr, 0, len - 1);
}
void Show(int* arr,int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 6,4 ,7,8,10,-5,90,34,55};
Show(arr,sizeof(arr) / sizeof(arr[0]));
printf("\n ");
QuickSort(arr, sizeof(arr) / sizeof(arr[0]));
Show(arr, sizeof(arr) / sizeof(arr[0]));
}
运行结果:

边栏推荐
- uniapp打包次数限制怎么办?只需两步就能解决
- Nacos集群的搭建过程详解
- el-progress implements different colors of the progress bar
- Detailed explanation of ten solutions across domains (summary)
- LaTeX使用frame制作PPT图片没有标号
- numpy.random使用文档
- docker部署完mysql无法连接
- 滚动条问题,未解决
- lingo入门——河北省第三届研究生建模竞赛B题
- LeetCode practice and self-comprehension record (1)
猜你喜欢

多用户商城多商户B2B2C拼团砍价秒杀支持小程序H5+APP全开源

LeetCode practice and self-comprehension record (1)

Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award

获取预训练模型的网络输入尺寸

【FAQ】CCAPI Compatible EOS Camera List (Updated in August 2022)

cs231n learning record
![In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]](/img/b5/03f55bb9058c08a48eae368233376c.png)
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]

LeetCode刷题记录(2)

多行文本省略

NB-IOT智能云家具项目系列实站
随机推荐
product learning materials
The hook of the operation of the selenium module
## 简讲protobuf-从原理到使用
NAT experiment
Nacos配置服务的源码解析(全)
获取预训练模型的网络输入尺寸
盒子模型大详解
VRRP overview and experiment
scikit-image图像处理笔记
盒子模型小练习
uniapp打包次数限制怎么办?只需两步就能解决
摆脱极域软件的限制
花花省V5淘宝客APP源码无加密社交电商自营商城系统带抖音接口
D39_Eulerian Angles and Quaternions
LaTeX笔记
D41_buffer pool
What is the website ICP record?
Tips for formatting code indentation
MyCat安装
Shadowless Cloud Desktop