快速排序

来源:互联网 发布:阿里云优势 编辑:程序博客网 时间:2024/05/17 03:52

 

#include<iostream>
using namespace std;
int pa(int a[],int p,int r)
{
 int i=p;
 int x=a[r];
 for(int j=p;j<r;j++)
 {
  if(a[j]<=x)
  {
  
   int temp;
   temp=a[i];
   a[i]=a[j];
   a[j]=temp;
       i++;
  }
 }
 int temp1;
 temp1=a[r];
 a[r]=a[i];
 a[i]=temp1;
 return i;
}
int qsort(int a[],int p,int r)
{
 if(p<r)
 {
  int q=pa(a,p,r);
  qsort(a,p,q-1);
  qsort(a,q+1,r);
 }
 return 0;
}

int main()
{
 int a[100],i=0,k;
 for(i=0;i<100;i++)
 {
  cin>>a[i];
  if(cin.get ()=='\n')
   break;
 }
 qsort(a,0,i);
 for(k=0;k<=i;k++)
  cout<<a[k]<<' ';
 cout<<endl;
 return 0;
}