2017.11.8. 快速排序

本文详细介绍了一种高效的排序算法——快速排序,并提供了完整的C++实现代码。通过对快速排序过程的逐步解析,帮助读者理解其工作原理及应用场合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

快速排序

适合题型:

1.模拟快速排序

样题: 快速排序

std.cpp:

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;

inline int read()
{
    int X=0,w=1; char ch=0;
    while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}

int sort(int a[],int l,int r)
{
    int i,j,x;

    i=l;  j=r;
    x=a[(l+r)>>1];   

    do{
        while (a[i]<x) ++i;
        while (a[j]>x) --j; 
        if(i<=j)
        {
            swap(a[i],a[j]);
            ++i;  --j; 
        }
    }while (i<=j);

    if (l<j) sort(a,l,j);
    if (i<r) sort(a,i,r); 
}

const int kkk=100050;
int n,a[kkk];
int main()
{
    n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a,1,n);
    for(int i=1;i<=n;i++)cout<<a[i]<<" ";   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值