当前位置:网站首页>Quick sort_ sort
Quick sort_ sort
2022-06-22 15:47:00 【Stephen_ Curry___】
Quick sort
The algorithm idea of quick sort I want to introduce today is the divide and conquer idea , This can be achieved in three steps ;
1. Determine the cut-off point x:
The commonly used value dividing point is : q[l] q[(l+r)/2] q[r];
2. Adjustment interval ( The core step ):
Make all the sections to the left of the dividing point <=x, All sections to the right of the dividing point >=x;
3. Recursive left and right intervals ;
Core code
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);
}
Put on a classic fast board problem
Input
The first line is an integer n(1<=n<=100000)
The second line is n Integers separated by spaces qi(0<=qi<=10^9)
Output
Output the result of ascending sort in one row .
#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;
}
This is the basic quick sort introduced today , It can greatly reduce the time complexity of the algorithm , After learning the new algorithm, you'd better find some problem brushes , Keep the thought and board firmly in mind to achieve a stable effect .
边栏推荐
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- 【题目精刷】2023禾赛-FPGA
- 微信小程序头像挂件制作
- FreeRTOS task priority and interrupt priority
- New load balancing webclient CRUD
- Reconstruction practice of complex C-end project of acquisition technology
- Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!
- What does Alibaba cloud's cipu release mean for enterprise customers?
- How to use the concat() function of MySQL
- New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
猜你喜欢

壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代

阿里云中间件的开源往事
The MIHA tour club in June is hot! 500+ posts, more than HC, just this summer (with internal promotion method)

Database connection pool: stress testing

What does password security mean? What are the password security standard clauses in the ISO 2.0 policy?
![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;

再次认识 WebAssembly

(pytorch进阶之路二)word embedding 和 position embedding

英国考虑基于国家安全因素让Arm在伦敦上市

Method of using inout signal in Verilog
随机推荐
After 17 years, Liu Yifei became popular again: ordinary people don't want to be eliminated, but they also need to understand this
C language learning -17- function is passed in as a parameter
历时100天、小鱼搭建了个机器人交流社区!!现公开邀请版主中!
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
洛谷P2466 [SDOI2008] Sue 的小球 题解
HMS Core新闻行业解决方案:让技术加上人文的温度
Meet webassembly again
类似attention nlp
打新债安全性有多高
Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization
推進兼容適配,使能協同發展 GBase 5月適配速遞
DDD understanding of Domain Driven Design
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
2020年蓝桥杯省赛真题-走方格(DP/DFS)
Scala语言学习-06-传名参数、传值参数、传函数参数的区别
专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!
The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
向量1(类和对象)
标准化、最值归一化、均值归一化应用场景的进阶思考
Is pioneer futures reliable? How to open a futures account safely?