浙大数据结构:编程练习 09-排序2 Insert or Merge (25 分)

09-排序2 Insert or Merge (25 分)

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 (≤100). 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<iostream>
#define MaxSize 100
int a[MaxSize];
int res[MaxSize];
using namespace std;

void GetData(int a[], int N) {
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
}

void CopyArr(int a[], int b[], int N) {
	/*将a[]数组拷贝到b[]数组,数组大小为N*/
	for (int i = 0; i < N; i++) {
		b[i] = a[i];
	}
}

int CompareRes(int a[], int res[],int N) {
	/*比较a[]和res[],相同则返回1,不同则返回0*/
	int Flag_IsSame = 1;
	for (int i = 0; i < N; i++) {
		if (a[i] != res[i]) {
			Flag_IsSame = 0;
			break;
		}
	}
	return Flag_IsSame;
}

void InsertSort(int tmpArr[], int i) {
	/*对tmpArr[]数组做第i次插入排序*/
	int tmp = tmpArr[i];
	int j;
	for (j = i; j > 0 && tmp < tmpArr[j - 1]; j--) {
		tmpArr[j] = tmpArr[j - 1];
	}
	tmpArr[j] = tmp;
}

void MergeArr(int a[], int tmp[], int L,int R,int R_end) {
	/*将数组a[],有序区间[L,R-1],[R,R_end]两个区间进行不断归并,然后存入到tmp[]中*/
	int L_end = R - 1;
	int ptr2tmp=L;
	while(L <= L_end&&R <= R_end) {
		if (a[L] < a[R]) {
			tmp[ptr2tmp++] = a[L++];
		}
		else {
			tmp[ptr2tmp++] = a[R++];
		}
	}
	while (L <= L_end) {
		tmp[ptr2tmp++] = a[L++];
	}
	while (R <= R_end) {
		tmp[ptr2tmp++] = a[R++];
	}
}

void MergeSort(int a[], int res[], int N, int length) {
	/*将长度为N的a[],以length为长度归并,然后存入到tmp中*/
	int i;
	for (i = 0; i + 2 * length < N; i = i + 2 * length) {
		MergeArr(a, res, i, i + length, i + 2 * length - 1);
	}
	/*剩余有序子列的合并*/
	if(i+length<N){
		MergeArr(a, res, i, i + length, N - 1);
	}
	else {
		CopyArr(a + i, res + i, N - i);
	}
}

int InsertOrMerge(int a[], int res[], int N) {
	/*多一次迭代的数组结果存放在res[]中*/
	int* tmpArr = new int[N];
	int* ComArr = new int[N];
	int InsertOrMerge=-1;//初始设置为-1,如果是Insert返回1,是Merge返回2;
	CopyArr(a,tmpArr,N);//初始化tmparr数组
	CopyArr(res, ComArr, N);
	int IterInsert,IterMerge;
	for (IterInsert = 1; IterInsert < N; IterInsert++) {
		InsertSort(tmpArr, IterInsert);
		if (CompareRes(tmpArr, ComArr,N) == 1) {
			InsertOrMerge = 1;
			InsertSort(tmpArr, IterInsert+1);//如果是插入排序,那么再执行一次迭代,然后跳出循环
			CopyArr(tmpArr, res,N);//InsertSort多一次迭代结果覆盖res[]
			break;
		}
	}

	CopyArr(a, tmpArr, N);//再次用a[]初始化tmpArr[],进行后续的判断
	int length = 1;
	IterMerge = 0;
	while (length <N) {
		MergeSort(a, tmpArr, N, length);
		CopyArr(tmpArr, a, N);//将数组拷贝回去,方便循环
		length = length * 2;
		IterMerge++;
		if (CompareRes(tmpArr, ComArr, N) == 1) {
			InsertOrMerge = 2;
			MergeSort(a, tmpArr, N, length);//如果是归并排序,那么再执行一次迭代,然后跳出循环
			CopyArr(tmpArr, res, N);//MergeSort多一次迭代结果覆盖res[]
			break;
		}
	}
	delete[]tmpArr;
	delete[]ComArr;
	if (InsertOrMerge == 1) return 1;
	if (InsertOrMerge == 2) return 2;
}


void DispRes(int Inser_Merge,int res[],int N) {
	if (Inser_Merge == 1) {
		cout << "Insertion Sort" << endl;
	}
	if (Inser_Merge == 2) {
		cout << "Merge Sort" << endl;
	}
	int i;
	for ( i = 0; i < N-1; i++) {
		cout << res[i]<<" ";
	}
	cout << res[i];
}

int main() {
	int N;
	cin >> N;
	GetData(a, N);//读取原始数据到数组a[]
	GetData(res, N);//读取排序后数据到数组res[]
	int InOrMeg;
	InOrMeg = InsertOrMerge(a, res, N);
	DispRes(InOrMeg, res,N);
	return 0;
}

测试结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值