# include<iostream>
void swap(int *a,int *b)//注意要传地址
{
int tmp = 0;
tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int data[],int start,int end )//最后是 i到 j-1 的都是小于 data[end]
{
int i ,j = -1;
//data[0] = 1000;
if(start < end)
{
for(j = start,i = start;i < end; i ++)
{
if(data[i]<data[end])
{
swap(&data[i],&data[j]);
j++;
}
}
swap(&data[end],&data[j]);
}
return j;
}
int partition1(int data[],int start,int end )//最后是 i到 j-1 的都是小于 data[end]
{
int i ,j = -1;
//data[0] = 1000;
if(start < end)
{
for(j = start+1,i = start+1;i <= end; i ++)
{
if(data[i]<data[start])
{
swap(&data[i],&data[j]);
j++;
}
}
swap(&data[start],&data[j]);
}
return j;
}
void quicksort(int data[],int start,int end)
{
int mid = 0;
if(start< end)
{
mid = partition1(data,start,end);
quicksort(data,start,mid-1);
quicksort(data,mid+1,end);
}
}
void printf(int data[])
{
int len = 10,i ;
for(i = 0;i < len ; i ++)
printf("%d ",data[i]);
}
int main()
{
int data[10] = {10,-232,43422,5,632,72,3,55,12,132};
quicksort(data,0,9);
printf(data);
return 0;
}
posted on 2010-07-31 00:10
付翔 阅读(165)
评论(0) 编辑 收藏 引用 所属分类:
排序