快速排序

来源:互联网 发布:手机淘宝怎么降价通知 编辑:程序博客网 时间:2024/05/21 23:33
#include<iostream>using namespace std;int a[100];int n;int partition(int r[],int low,int high)// 这只是一次划分,还要进行递归排序。 {   r[0] = r[low];   int pivotkey = r[low];   while(low < high)   { while(low < high && r[high] >= pivotkey) --high; if(low < high) r[low++] = r[high]; while(low < high && r[low] <= pivotkey)   ++low; if(low < high) r[high--] = r[low];   }   r[low] = r[0];   return low;}void Qsort(int  r[],int s,int t){if(s<t){int pivotloc = partition(r,s,t);Qsort(r,s,pivotloc-1);Qsort(r,pivotloc+1,t);}}int main(){while(cin>>n){for(int i = 1; i <= n; i ++)cin>>a[i];Qsort(a,1,n); for(int i = 1; i <= n; i ++)       cout<<a[i]<<" ";        cout<<endl;}return 0;}

0 0
原创粉丝点击