快排

本文详细介绍了一种高效的排序算法——快速排序。通过递归的方式,在数组中选择一个基准元素,并将数组分为两部分,一部分包含小于基准的所有元素,另一部分包含大于基准的所有元素。最后通过递归调用该过程,实现整个数组的排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void quickSort(int * const a, int startIndex, int endIndex)
{
	int s = startIndex;
	int e = endIndex;
	if (endIndex == startIndex )
		return;
	endIndex--;
	while (startIndex < endIndex)
	{
		int temp = a[startIndex];
		while (startIndex < endIndex)
		{
			if (a[endIndex] <= temp)
			{
				break;
			}
			endIndex--;
		}
		if (startIndex < endIndex)
		{
			//有比startIndex位置小的数
			a[startIndex] = a[endIndex];
			a[endIndex] = temp;
		}
		//从startIndex开始往后找,找一个比temp大的数,此时endIndex上放的是temp
		temp = a[endIndex];
		startIndex++;
		while (startIndex < endIndex)
		{
			if (a[startIndex] > temp)
			{
				break;
			}
			startIndex++;
		}
		if (startIndex < endIndex)
		{
			a[endIndex] = a[startIndex];
			a[startIndex] = temp;
		}
	}
	//递归
	quickSort(a, s, startIndex);
	quickSort(a, startIndex + 1, e);
}

  

posted on 2017-05-18 12:57 好吧,就是菜菜 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6872780.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值