当前位置:网站首页>Quick sort
Quick sort
2022-06-12 09:07:00 【Record dada I】
Concept : Set a benchmark for each sorting , Place the number less than the reference point to the left of the reference point , The number greater than the reference point is placed to the right of the reference point , Make the exchange distance larger .
#include<stdio.h>
int a[100];// Global variables
int n;// Number of numbers to read
void quicksort(int left,int right) {
int i,j,t,temp;
if(left>right) {
return;
}
temp=a[left];// Benchmark number
i=left;
j=right;
while(i!=j) {
// Look from right to left
while(a[j]>=temp&&i<j) {
j--;
}
// Look from left to right
while(a[i]<=temp&&i<j) {
i++;
}
if(i<j) {
// If not, change the position
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
// The first position, i.e. the reference position, is interchanged with the middle position ( The last exchange of every search ).
a[left]=a[i];
a[i]=temp;
// Recursively handle left and right
quicksort(left,i-1);
quicksort(i+1,right);
return;
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
quicksort(1,n);
for(int i = 1;i<=n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
边栏推荐
- Résoudre le problème de demander à l'élément d'être ouvert lorsque l'unit é est ouverte et que vous n'avez pas été ouvert auparavant (peut - être fermé anormalement auparavant)
- Diff prime pairs
- Chapter 3 registers (memory access)
- (十五) TweenRunner
- Introduction Fibonacci series
- Chapter IV - first procedure
- (js)三位用逗号隔开,保留两位小数(or 四舍五入取整)
- [computer use] how to change a computer disk into a mobile disk?
- Box model border
- Jupyter notebook sets the default browser to open with an error syntaxerror: (Unicode error) 'UTF-8' codec can't decode byte 0xd4
猜你喜欢

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

MySQL learning record - II. MySQL create table command

Flink传入自定义的参数或配置文件

Summary of common character sets

Description of string

Box model border

Redis installation test

2022 safety officer-c certificate special operation certificate examination question bank and simulation examination

Building a cluster: and replacing with error

最少换乘次数
随机推荐
torch. logical_ And() method
长安链节点证书、角色、权限管理介绍
Application method of new version UI of idea + use method of non test qualification and related introduction
EIP-1559
Wechat applet image saving function
xshell启动遇到“由于找不到mfc110.dll,无法继续执行代码的解决方法”
Popular understanding of time domain sampling and frequency domain continuation
Union selector
Does database and table splitting cause reading diffusion problems? How to solve it?
Machine learning notes - circular neural network memo list
Chapter 7 - more flexible location of memory addresses
Flink passes in custom parameters or profiles
Problems that cannot be resolved by tar command
Filters and listeners
清华大学数据挖掘笔记(一)
The database doesn't know what went wrong
Résoudre le problème de demander à l'élément d'être ouvert lorsque l'unit é est ouverte et que vous n'avez pas été ouvert auparavant (peut - être fermé anormalement auparavant)
Notes on data mining in Tsinghua University (1)
Definition of polar angle and its code implementation
Solve the problem that when unity is opened, you will be prompted that the project has been opened, but you have not opened it before (it may be closed abnormally before)