快速排序 qsort

void quicksort(int *arr,int ileft,int iright)
{
	if(ileft<iright)
	{
		int ipivot = arr[ileft];
		int i = ileft;
		int j = iright;
		while(i<j)
		{
			while(i<j && arr[j]>ipivot)
			{
				j--;
			}
			arr[i] = arr[j];
			while(i<j && arr[i]<ipivot)
			{
				i++;
			}
			arr[j] = arr[i];
		}
		arr[i] = ipivot;
		quicksort(arr,ileft,i-1);
		quicksort(arr,i+1,iright);
	}
}

void main()
{
	int arr[] = {2,323,1,5,78,34,50};//没有优化重复值
	quicksort(arr,0,6);
	for (int i=0;i<7;++i)
	{
		printf("%d\n",arr[i]);
	}
	getchar();
}

你可能感兴趣的:(C++)