当前位置:网站首页>快速排序quick_sort
快速排序quick_sort
2022-06-22 14:26:00 【Stephen_Curry___】
快速排序
今天我要介绍的快速排序的算法思想是分治思想,具体可以通过三步来实现;
1.确定分界点x:
常用的取值分界点是: q[l] q[(l+r)/2] q[r];
2.调整区间(核心步骤):
使分界点以左区间全部<=x,分界点以右区间全部>=x;
3.递归左右区间;
核心代码
void quick_sort(int q[],int l,int r)
{
if(l>=r)return;
int x=q[l],i=l-1,j=r+1;
while(i<j)
{
do i++;while(q[i]<x);
do j--;while(q[j]>x);
if(i<j)swap(q[i],q[j]);
}
quick_sort(q,l,j);
quick_sort(q,j+1,r);
}
放个经典快排板子题
输入
第一行是一个整数n(1<=n<=100000)
第二行是n个以空格隔开的整数qi(0<=qi<=10^9)
输出
在一行内输出升序排序的结果。
#include<iostream>
using namespace std;
const int N = 1e6+10;
int q[N];
int n;
void quick_sort(int q[],int l,int r)
{
if(l>=r)return;
int x=q[l],i=l-1,j=r+1;
while(i<j)
{
do i++;while(q[i]<x);
do j--;while(q[j]>x);
if(i<j)swap(q[i],q[j]);
}
quick_sort(q,l,j);
quick_sort(q,j+1,r);
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&q[i]);
quick_sort(q,0,n-1);
for(int i=0;i<n;i++)printf("%d ",q[i]);
return 0;
}
这就是今天介绍的最基本的快速排序啦,可以大大的降低算法时间复杂度,学习完新的算法之后最好找些题刷,把思想和板子牢牢记在心里达到稳固的效果。
边栏推荐
- 个人免签支付方案推荐
- 晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
- FreeRTOS task priority and interrupt priority
- SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
- 还整成这样
- 专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
- The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
- Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
- 2022年失业的人多吗?今年是不是特别难找工作?
- keil MDK 中使用虚拟串口调试串口
猜你喜欢

曾经,我同时兼职5份工作,只为给女友买个新款耳环......

数据库连接池:连接池功能点的实现

Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
![Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;](/img/75/d2ad171d49611a6578faf2d390af29.jpg)
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?

Ros2 pre basic tutorial | Xiaoyu teaches you how to use cmake dependency lookup process
![[Zhejiang University] information sharing of the first and second postgraduate entrance examinations](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Zhejiang University] information sharing of the first and second postgraduate entrance examinations

Those confusing user state & kernel state

Good wind relies on strength – code conversion practice of accelerating SQL Server Migration with babelfish

NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
随机推荐
还可以这样搞nlp(分类)
ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
【浙江大学】考研初试复试资料分享
NF-ResNet:去掉BN归一化,值得细读的网络信号分析 | ICLR 2021
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
U++ operator learning notes
Show me my personal work list for the past two years. I earn 6K a month in my spare time. It's so delicious to have a sideline
What happened to those who didn't go to college
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
UE4 obtains local files through blueprints
山东泰安“6·21”燃气爆炸事故后续:全面排查整治餐饮场所燃气安全隐患
Reconstruction practice of complex C-end project of acquisition technology
个人免签支付方案推荐
Reading of double pointer instrument panel (II) - Identification of dial position
百行代码实现基于Redis的可靠延迟队列
大佬们好,初次使用MySQL cdc 报错
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
Those confusing user state & kernel state
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
mysql如何修改存储引擎为innodb