C语言归并排序

C语言归并排序,第1张

C语言归并排序
#include
int temp[100000];
int a[100000];
void guibing2(int low, int mid, int high)
{
	int i, j, p;
	for (i = low, j = mid, p = low; i < mid && j <= high; p++)
	{
		if (a[i] < a[j])
		{
			temp[p] = a[i++];
		}
		else
		{
			temp[p] = a[j++];
		}
	}
	while (i < mid)
	{
		temp[p++] = a[i++];
	}
	while (j <= high)
	{
		temp[p++] = a[j++];
	}
	for (i = low; i <= high; i++)
	{
		a[i] = temp[i];
	}
}
void guibing1(int low, int high)
{
	if (high > low)
	{
		int mid = (high + low) / 2;
		guibing1(low, mid);
		guibing1(mid + 1, high);
		guibing2(low, mid+1, high);
	}
}
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
	}
	guibing1(1, n);
	for (i = 1; i <= n; i++)
	{
		printf("%d ",a[i]);
	}
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713363.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存