快速排序(模板)

#include<iostream>
using namespace std;
const int maxn = 1e+5;
int a[maxn];
void quick_Sort(int *a,int low,int high)
{
        if(low >= high) return ;
        int i = low;
        int j = high;
        int key = a[low];
        while(i < j)
        {
                while(i <j && key <= a[j])
                {
                        j--;
                }
                a[i] = a[j];
                while(i < j && key >= a[i])
                {
                        i++;
                }
                a[j] = a[i];
        }
        a[i] = key;
        quick_Sort(a, low,i - 1);
        quick_Sort(a,i + 1,high);

}
int main()
{
   int n;
   cin>>n;
   for (int i = 0;i < n;i++)
        cin >> a[i];
   quick_Sort(a,0,n-1);

   for (int i=0;i < n;i++)
        cout<<a[i]<<" ";
}

阅读更多
换一批

没有更多推荐了,返回首页