快速排序 C语言

//归并算法
void swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}
void sort(int b[], int s,int e)
{
	if (s >= e)
		return;
	int k = b[s];
	int i = s;
	int j = e;
	while (i != j)
	{
		while (i < j && b[j] >= k)
			--j;
		swap(&b[i], &b[j]);
		while (i < j && b[i] <= k)
			++i;
		swap(&b[i], &b[j]);
	}
	sort(b,s,i-1);
	sort(b,i+1,e);
}




int main()
{



	/*快速排序 分治 归并算法*/
	int b[9] = { 7, 9, 9, 4, 3, 1, 2, 9, 8 };
    sort(b, 0, 8);
	for (int i = 0; i < 9; ++i)
	{
		printf("%d", b[i]);
         }
}

猜你喜欢

转载自blog.csdn.net/qq_41750725/article/details/79947340