当前位置:网站首页>Quick sort_ sort
Quick sort_ sort
2022-06-21 08:50: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 .
边栏推荐
- Shortcut keys accumulated when using various software
- An aunt's towel holds up the 100 billion market behind 400million Chinese women
- 积分签到任务设置的要求,积分商城搭建过程中常见的问题
- Can you implement these requirements with MySQL
- Decrypt FTP
- Detailed analysis of abstractqueuedsynchronizer (AQS) source code - Analysis of lock release and response interrupt locking processes
- Abstractqueuedsynchronizer (AQS) source code detailed analysis - countdownlatch source code analysis
- tidb4.0.0遇见的问题、报错总结(tiup部署)
- Retrofit Extended reading
- STL tutorial 2-myarray framework implementation
猜你喜欢

请问这些要求用mysql能实现吗

window10局域网共享文件夹流程

leetcode:19. 删除链表的倒数第 N 个结点

Detailed analysis of ThreadPoolExecutor source code of thread pool

Joking Domain Driven Design (VI) -- Boundary context -- Design

Audio immersive experience

声临其境 — 音频沉浸体验

The difference between tuples and lists

FD:文件描述符

26. Hikvision camera configuration and preliminary test
随机推荐
4.8 inquirer-autocomplete-prompt
Extensions in kotlin
[DB written interview 274] in Oracle, what is deferred segment creation?
[MySQL performance optimization] - optimize query
An app developed based on retrotfit2.1+material design+ijkplayer
Source insight shortcut key cross reference
Abstractqueuedsynchronizer (AQS) source code detailed analysis - lock competitive resource analysis
TiDB3.0- 4.0 内存控制/修改日志保存天数/最大索引长度
如何使用 adb shell 查询进程流量情况
Is it safe to open a stock account at present? Can I open an account online directly?
4.4 Eval function replaces function
Improve code checking with annotations
4.7 Inquirer. JS usage example
Requirements for setting up points sign in tasks and common problems in the process of building points mall
Unity .net 框架问题
Given a two-dimensional list of m*n, find out whether a number exists
PS prompts "script error -50 general Photoshop error, how to solve it?
This article takes you to interpret the advertising advantages of tiktok
Client construction and Optimization Practice
The next stop of Intelligent Manufacturing: cloud native + edge computing two wheel drive