当前位置:网站首页>快速排序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;
}
这就是今天介绍的最基本的快速排序啦,可以大大的降低算法时间复杂度,学习完新的算法之后最好找些题刷,把思想和板子牢牢记在心里达到稳固的效果。
边栏推荐
- 架构师之路,从「存储选型」起步
- No wonder the postgraduate entrance examination is so hot. These are the "hidden benefits" of Postgraduates!
- Is it difficult for flush to open an account? Is it safe to open an account online?
- NF-ResNet:去掉BN归一化,值得细读的网络信号分析 | ICLR 2021
- 『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
- OOP 多重收纳(类模板)
- Mitsubishi manipulator demo program
- Phpstudy 2016 build Pikachu range
- SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
- Reading of double pointer instrument panel (II) - Identification of dial position
猜你喜欢

PHP内置协议(支持的协议和封装协议)

Database connection pool: implementation of connection pool function point

New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer

数据资产管理:数据发现,发现什么,怎么发现?

Please, don't be brainwashed. This is the living reality of 90% of Chinese people

天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
![[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide](/img/46/d36ae47c3d44565d695e8ca7f34980.jpg)
[live broadcast review] battle code pioneer phase VI: build a test subsystem and empower developers to provide

Recommendation of individual visa free payment scheme

Method of using inout signal in Verilog

MongoDB在腾讯零售优码中的应用
随机推荐
向量3(静态成员)
What does Alibaba cloud's cipu release mean for enterprise customers?
[Software Engineering] planning and project management
What are the five characteristics of network security? What are the five attributes?
Bochs software usage record
Ask if you want to get the start of sqlserver_ Is there a good way for LSN?
mysql如何将字段修改为not null
乱解码nlp
得物技术复杂 C 端项目的重构实践
flutter video_player实现监听和自动播放下一首歌曲
Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
[Software Engineering] design module
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
KEIL仿真和vspd
好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
推荐几个AI智能平台
Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
U++ 迭代 排序 查询 学习笔记
At 19:00 this Thursday evening, the 7th live broadcast of battle code Pioneer - how third-party application developers contribute to open source