快速排序

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
void print(int a[],int n)
{
    for(int i=0;i<n;i++) cout << a[i] << '	';
    cout << endl;
}
void quickSort(int a[],int L,int R)
{
    int mid = a[(L+R)/2];
    int i=L,j=R;
    while(i<=j)
    {
        while(a[i]<mid) i++;
        while(a[j]>mid) j--;
        if(i<=j) swap(a[i++],a[j--]);
    }
    if(L<j) quickSort(a,L,j);
    if(i<R) quickSort(a,i,R);
}
int main(void)
{
    freopen("D:\1.txt","r",stdin);
    int n,a[100];
    cin >> n;
    for(int i=0;i<n;i++) cin >> a[i];
    print(a,n);
    quickSort(a,0,n-1);
    print(a,n);
    return 0;    
} 
原文地址:https://www.cnblogs.com/zuimeiyujianni/p/10461507.html