快速排序法

#include <iostream>

 

void QSort(int [],int,int);

void main(){

  int arry[]={4,25,32,14,45,12,12,78,45,12,99,65,98,74,3};

  int len=sizeof(arry)/sizeof(int);

 

  for (int i=0;i<len;i++) cout << arry[i] << ",";

  cout << endl;

 

  QSort(arry,0,len-1);

 

  for (i=0;i<len;i++) cout << arry[i] << ",";

  cout << endl;

}

 

void QSort(int a[],int left,int right){

  int l,r,m,t;

  l=left;

  r=right;

  m=a[(left+right)/2];

 

  while(l<r){

       while(a[l]<m)l++;

       while(a[r]>m)r--;

       if(l>=r)break;

 

       t=a[l];

       a[l]=a[r];

       a[r]=t;

 

       if(a[l]!=m)l++;

       r--;

  }

 

  if(l==r)l++;

  if(left<r)QSort(a,left,r);

  if(l<right)QSort(a,l,right);

}

 

你可能感兴趣的:(include)