快速排序

梦想游戏人
目录:
algorithm
/// http://developer.51cto.com/art/201403/430986.htm

void quickSort(int * a, int left, int right)
{

	int i = left, j = right,key= a[left];
	if (left > right)return;//终止条件

	while (i < j)
	{

		while (a[j] >= key && i < j) --j; //先扫描右边开始 直到找到比基数小的

		while (a[i] <= key && i < j) ++i; // 左边开始找直到比基数大

		if (i < j)//满足交换2者的条件
		{
			int t = a[i];
			a[i] = a[j];
			a[j] = t;
		}

	}

	a[left] = a[i]; // 基数交换到i j相遇点
	a[i] = key;
	quickSort(a, left, i - 1);//递归左区间
	quickSort(a,j + 1, right);//递归右区间
}


	int a[] = { 10, 2, 7, 2, 4, 9, 6 };
	quickSort(a, 0, 6);
	for (int i = 0; i <= 6; i++)
	{
		cout << a[i]<<" ";
	}

http://developer.51cto.com/art/201403/430986.htm

Scroll Up