数据结构 09-排序2 Insert or Merge(C语言)

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Sample Input 1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output 2:

Merge Sort
1 2 3 8 4 5 7 9 0 6

code

# include <cstdio>

int n;
typedef enum { Insertion, Merge } sortMethod;
int initialSeq[110];
int sortSeq[110];

int min(int a, int b) { return a < b ? a : b; }

int orderedlength()
{
	for (int i = 0; i < n - 1; i++)
	{
		if (sortSeq[i] > sortSeq[i + 1]) return i + 1;
	}
	return n;
}

void distinguish(sortMethod & M, int & iteration)
{
	// 判断排序类型

	int p = orderedlength();
	int i;
	for (i = p; i < n; ++i)
		if (sortSeq[i] != initialSeq[i]) break;

	if (i == n) M = Insertion;
	else M = Merge;


	// 计算排到第几趟了
	if (M == Insertion)
	{
		iteration = p;
	}
	else
	{
		iteration = 64;

		for (bool current_iter = false; !current_iter; iteration /= 2) // 归并排序的有序序列最大长度,从64开始验证
		{
			if (iteration > n) continue;
			current_iter = true;

			int cnt = n / iteration; // 完整有序序列的个数

			if (n - cnt * iteration > iteration / 2) cnt++; //尾部不完整有序序列

			for (int p = 0; current_iter && p < cnt; ++p)
			{
				for (int j = iteration * p; j < iteration*(p + 1) - 1 && j < n - 1 && current_iter; ++j)
				{
					if (sortSeq[j] > sortSeq[j + 1]) current_iter = false;
				}
			}

			if (current_iter) break;
		}
	}
}


void InsertionSortGoOn(int iteration)
{
	int X = sortSeq[iteration], i;
	for (i = iteration; i > 0 && sortSeq[i - 1] > X; --i)
	{
		sortSeq[i] = sortSeq[i - 1];
	}
	sortSeq[i] = X;
}

void MergeSortGoOn(int iteration)
{
	int cnt = n / iteration / 2; //归并多少次
	if (n - cnt * iteration * 2 > iteration) ++cnt;

	int A[110]; // 辅助数组

	for (int i = 0; i < cnt; ++i)
	{
		// (  i*iteration ~ i*iteration*2+iteration  ]   与  (   i*iteration*2+iteration ~ min(n, (i+1)*iteration*2)    ]   归并
		int p1 = i * iteration*2, p2 = i*iteration*2+iteration, pA = i * iteration*2;
		int p1end = i * iteration * 2 + iteration;
		int p2end = min(n, (i + 1)*iteration * 2);

		while (p1!=p1end && p2 != p2end)
		{
			if (sortSeq[p1] < sortSeq[p2])
				A[pA++] = sortSeq[p1++];
			else
				A[pA++] = sortSeq[p2++];
		}
		while (p1 != p1end) A[pA++] = sortSeq[p1++];
		while (p2 != p2end) A[pA++] = sortSeq[p2++];

		for (int j = i * iteration*2; j < p2end; ++j) sortSeq[j] = A[j];
	}
}

int main(void)
{
	scanf("%d\n", &n);
	for (int i = 0; i < n; ++i) scanf("%d", &initialSeq[i]);
	for (int i = 0; i < n; ++i) scanf("%d", &sortSeq[i]);
	sortMethod M;
	int iteration;
	distinguish(M, iteration);
	if (M == Insertion)
	{
		printf("Insertion Sort\n");
		InsertionSortGoOn(iteration);
	}
	else
	{
		printf("Merge Sort\n");
		MergeSortGoOn(iteration);
	}

	for (int i = 0; i < n; ++i)
		printf("%d%s", sortSeq[i], i==n-1?"\n":" ");

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值