首页 > 其他 > 详细

排序算法--快速排序

时间:2014-02-15 00:07:42      阅读:407      评论:0      收藏:0      [点我收藏+]

#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;
}

排序算法--快速排序

原文:http://www.cnblogs.com/WangYinlong/p/3549108.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!