当前位置:网站首页>快速排序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;
}
这就是今天介绍的最基本的快速排序啦,可以大大的降低算法时间复杂度,学习完新的算法之后最好找些题刷,把思想和板子牢牢记在心里达到稳固的效果。
边栏推荐
- Keil simulation and VSPD
- 全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer
- 社区文章|MOSN 构建 Subset 优化思路分享
- Bochs software usage record
- 得物技术复杂 C 端项目的重构实践
- Go all out to implement the flood control and disaster relief measures in detail and in place, and resolutely protect the safety of people's lives and property
- 晒晒我这两年的私活单,业余时间月入6k,有份副业也太香啦
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- 那些没考上大学的人,后来过的怎样
- Sword finger offer46 -- translate numbers into strings
猜你喜欢

How to use the concat() function of MySQL

Database connection pool: implementation of connection pool function point

mysql的concat()函数如何用

Token processing during API encapsulation

Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!

mysql如何修改存储引擎为innodb

多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中

Verilog使用inout信号的方法

After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!

The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
随机推荐
米哈游六月社招火热开启!500+岗位,超多HC,就在这个夏天(附内推方式)
Software architecture
Recommendation of individual visa free payment scheme
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
phpStudy 2016搭建-pikachu靶场
问一下想获取到sqlserver的start_lsn有好的办法吗?
KEIL仿真和vspd
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
[Software Engineering] acquire requirements
三菱机械臂demo程序
那些令人懵逼的用户态&内核态
基于最小化三维NDT距离的快速精确点云配准
百行代码实现基于Redis的可靠延迟队列
【浙江大学】考研初试复试资料分享
封装api时候token的处理
数据库连接池:压力测试
接了个私活项目,一下赚了15250,还有必要做主业吗?
大会倒计时 | 亚马逊云科技创新大会邀您一起构建AI新引擎 !
Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node