C - Quick Sort (one of the simplest)

44 篇文章 1 订阅

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击人工智能教程

/*
 * Quick Sort:
 *
 * Given an array, one element is chosen and the others partitioned in two subsets
 * - those less than the partition element and those greater than or equal to it.
 * The same process is then applied recursively to the two subsets. When a subset
 * has fewer than two elements, it doesn't need any sorting; this stops the recursion.
 *
 * This version of quicksort is not the fastest possible, but it's one of the simplest.
 * We use the middle element of each subarray for partitioning.
 *
 * QuickSort.c - by FreeMan
 */

 /* QuickSort: Sort v[left]...v[right] into increasing order */
void QuickSort(int v[], int left, int right)
{
	int i, last;
	void Swap(int v[], int i, int j);

	if (left >= right) /* Do nothing if array contains fewer than two elements */
	{
		return;
	}
	Swap(v, left, (left + right) >> 1); /* Move partition element to v[left] */
	last = left;
	for (i = left + 1; i <= right; i++) /* Partition */
	{
		if (v[i] < v[left])
		{
			Swap(v, ++last, i);
		}
	}
	Swap(v, left, last); /* Restore partition element */
	QuickSort(v, left, last - 1);
	QuickSort(v, last + 1, right);
}

/* Swap: Interchange v[i] and v[j] */
void Swap(int v[], int i, int j)
{
	int temp;
	temp = v[i];
	v[i] = v[j];
	v[j] = temp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值