排序算法--快速排序

#include<iostream>

using namespace std;

void print(int *l,int length)
{
 for(int i = 0;i < length;i++)
 {
  cout<<l[i]<<"\t";
 }
 cout<<"\n";
}
int sort(int *l,int low,int high)
{
 int media = l[low];
 while(low < high)
 {
  while(low < high && l[high] >= media)
  high--;
  l[low] = l[high];
  while(low < high && l[low] <= media)
   low++;
  l[high] = l[low];
 }
 l[low] = media;
 return low;
}
void partition(int *l,int low,int high)
{
 int pos ;
 if(low < high)
 {
  pos = sort(l,low,high);
 // print(l,9);
  partition(l,low,pos-1);
  partition(l,pos+1,high);
 }
}

int main()
{
 int l[9]={8,5,7,4,6,2,3,1,9};
 int size = sizeof(l)/sizeof(int);
 cout<<"排序前数组元素为:"<<endl;
 print(l,size);

 partition(l,0,size-1);
 cout<<"排序后数组元素为:"<<endl;
 print(l,size);
 return 0;
}

posted @ 2014-02-14 10:19  c plus plus  阅读(121)  评论(0)    收藏  举报