09-排序2 Insert or Merge

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?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Merge Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

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

#include<stdlib.h>
#include<stdio.h>

int isSame(int *a, int *b, int length) {
	int m = 0;
	for (int i = 0; i < length; i++)
	{
		if (a[i] != b[i]) {
			m = 1;
		}
	}
	if (!m) {
		return 1;
	}
	else {
		return 0;
	}
}

//归并
void sort(int *a, int *b, int left, int right,int center) {
	if (left < right) {
		int Center = center;
		int A = left;
		int B = Center + 1;
		int K = left;
		while (A != Center + 1 && B != right + 1)
		{
			if (a[A] < a[B]) {
				b[K++] = a[A++];
			}
			else {
				b[K++] = a[B++];
			}
		}
		while (A != Center + 1) {
			b[K++] = a[A++];
		}
		while (B != right + 1)
		{
			b[K++] = a[B++];
		}
		for (int i = left; i <= right; i++)
		{
			a[i] = b[i];
		}
	}
}



int main()
{
	int n, m;
	scanf("%d\n", &n);
	int flag = 0;
	int a[101];
	int aa[101];
	int b[101];
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i]);
		aa[i] = a[i];
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &b[i]);
	}
	int temp;
	
	for (int i = 0; i < n; i++)
	{
		if (isSame(a, b, n)) {
			flag = 1;
			printf("Insertion Sort\n");
			int L;
			if (i == 0) {
				L = 2;
			}
			else {
				L = i;
			}
			if (L < n) {
				temp = a[L];
				for (int j = 0; j < L; j++)
				{
					if (a[j] > a[L]) {
						int z;
						for (z = L - 1; z >= j; z--)
						{
							a[z + 1] = a[z];
						}
						a[j] = temp;
						break;
					}
				}
			}
			for (int m = 0; m < n; m++)
			{
				printf("%d", a[m]);
				if (m != n - 1) {
					printf(" ");
				}
			}
			break;
		}
		temp = a[i];
		for (int j = 0; j < i; j++)
		{
			if (a[j] > a[i]) {
				int z;
				for (z = i-1; z >= j; z--)
				{
					a[z + 1] = a[z];
				}
				a[j] = temp;
				break;
			}
		}
	}
	if (flag == 0) {
		int c[101];
		printf("Merge Sort\n");
		int length = 1;
		while (length<n)
		{
			int i = 0;
			if (isSame(aa, b, n)) {
				i = 0;
				length *= 2;
				for (i = 0; i + length - 1 < n;)
				{
					sort(aa, c, i, i + length - 1, (i + i + length - 1) / 2-1);
					i += length;
				}
				if (n - i > length / 2) {
					sort(aa, c, i, n - 1, i + length / 2);
				}
				for (int m = 0; m < n; m++)
				{
					printf("%d", aa[m]);
					if (m != n - 1) {
						printf(" ");
					}
				}
				break;
			}
			i = 0;
			length *= 2;
			for (i = 0; i + length - 1 < n;)
			{
				sort(aa, c, i, i + length - 1,(i+i+length-1)/2);
				i += length;
			}
			if (n - i > length / 2) {
				sort(aa, c, i, n-1,i+length/2-1);
			}
			if (isSame(aa, b, n)) {
				i = 0;
				length *= 2;
				for (i = 0; i + length - 1 < n;)
				{
					sort(aa, c, i, i + length - 1, (i + i + length - 1) / 2);
					i += length;
				}
				if (n - i > length / 2) {
					sort(aa, c, i, n - 1, i + length / 2-1);
				}
				for (int m = 0; m < n; m++)
				{
					printf("%d", aa[m]);
					if (m != n - 1) {
						printf(" ");
					}
				}
				break;
			}
		}
	}
	
	system("pause");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`git branch --ff-only merge` 不是一个有效的 Git 命令。请注意,`--ff-only` 和 `merge` 是独立的选项和指令。 `git branch --ff-only` 是一个用于检查当前分支是否可以进行快进合并(fast-forward merge)的命令。快进合并是指将一个分支的提交历史直接应用到另一个分支上,而不会创建新的合并提交。如果使用 `git branch --ff-only` 检查当前分支时,结果为成功,则说明当前分支可以进行快进合并。 另一方面,`merge` 是一个用于将一个分支的更改合并到当前分支的命令。可以使用 `git merge <branch>` 来执行合并操作,其中 `<branch>` 是要合并的分支名称。 因此,如果你想将一个分支(例如 "merge" 分支)的更改合并到当前分支,并且只在快进合并可行时才进行合并,可以按照以下步骤操作: 1. 首先,确保你在要合并更改的目标分支上。你可以使用 `git checkout <target_branch>` 命令切换到目标分支。 2. 然后,运行 `git branch --ff-only` 命令来检查是否可以进行快进合并。 3. 如果 `git branch --ff-only` 命令返回成功,即当前分支可以进行快进合并,则运行 `git merge merge` 命令来将 "merge" 分支的更改合并到当前分支。 需要注意的是,合并操作可能会导致合并提交的生成,特别是当两个分支之间存在分叉点时。只有在可以进行快进合并的情况下,才会执行无合并提交的快进合并。否则,将创建一个新的合并提交来合并两个分支的更改。 总结起来,`git branch --ff-only` 是用于检查当前分支是否可以进行快进合并的命令,而 `git merge <branch>` 是用于将一个分支的更改合并到当前分支的命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值