现在的位置: 首页 > 综合 > 正文

快速排序

2013年12月12日 ⁄ 综合 ⁄ 共 464字 ⁄ 字号 评论关闭
#include <stdio.h>
int split(int *a,int low,int high)
{
	int pivo,i=low,j=high,x=a[low];
	while(i<j)
	{
		while(a[j]>x&&i<j)j--;
		if(i<j)
		{
			a[i]=a[j];
			i++;
		}
		while(a[i]<x&&i<j)i++;
		if(i<j)
		{
			a[j]=a[i];
			j--;
		}
	}
	a[i]=x;
	return i;
}
void quick_sort(int *a,int low,int high)
{
	int i;
	if(low<high)
	{
		i=split(a,low,high);
		quick_sort(a,low,i-1);
		quick_sort(a,i+1,high);
	}
}

void qsort(int *a,int n)
{
	quick_sort(a,0,n-1);
}
int main()
{
	
	int i,a[10]={1,2,3,4,5,6,7,8,12,10};
	qsort(a,10);
	for(i=0;i<10;i++)
	{
		printf("%d ",a[i]);
	}
	return 0;
}

抱歉!评论已关闭.