快速排序

void my_swap(int &a, int &b)
{
	int t = a;
	a = b;
	b = t;
}

int partion(int a[], int left, int right)
{
	int t = a[right];
	int i = left-1;
	int j = right;

	for(;;)
	{
		while (a[++i] < t);
		while (a[--j] > t) if (j <= left)  break;
		if (j <= i) break;
		my_swap(a[i], a[j]);

	}
	my_swap(a[right], a[i]);
	return i;
}

void quick_sort(int a[], int left, int right)
{
	if (left >= right) return;
	int mid = partion(a, left, right);
	quick_sort(a, left, mid-1);
	quick_sort(a, mid+1, right);
}


你可能感兴趣的:(快速排序)