浙大数据结构: 09-排序3 Insertion or Heap Sort (25 分)

09-排序3 Insertion or Heap Sort (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.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.

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 “Heap 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 resulting 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 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9

代码

#include<iostream>
#define MaxSize 100
typedef enum{Insert,Heap}TYPE;
using namespace std;
int BeforeSort[MaxSize];
int AfterSort[MaxSize];
void GetData(int a[],int N) {
	for (int i = 0; i < N; i++) {
		cin >> a[i];
	}
}


void InsertSort(int a[], int N, int tmp) {
	/*对a[N]数组从抽出第tmp个元素进行插入排序*/
	int i;
	int tval = a[tmp];
	for ( i = tmp; i > 0&&a[i-1]>tval; i--) {
		a[i] = a[i - 1];
	}
	a[i] = tval;
}

void PercDown(int a[], int start, int N) {
	/*数组a[]整最大堆堆,从0~N-1*/
	int tval = a[0];
	int parent, child;
	int beg = start;
	int end = N - 1;
	for (parent = start; parent * 2 + 1 <= end;parent=child) {
		child = parent * 2 + 1;
		if (child != end&&a[child] < a[child + 1]) {
			child++;
		}
		if (a[child] > tval) {
			a[parent] = a[child];
		}
		else
			break;
	}
	a[parent] = tval;
}

void HeapSort(int a[], int N) {
	/*对a[N]数组进行下一轮堆排序*/
	int tmp = N - 1;
	for (; a[tmp] > a[tmp - 1]; tmp--) {}//结束循环时,a[tmp]是无序的,要被替换
	int tval = a[tmp];
	a[tmp] = a[0];
	a[0] = tval;
	PercDown(a, 0, tmp);//交换后整堆,堆的规模就是tmp,因为第a[tmp]已经整理过了
}

TYPE InsertOrHeap(int b[], int a[],int N) {
	/*判断是插入还是堆排序,插入是前面有序,后面排序前后相同;堆排序是后面有序,前面不一定相同*/
	/*b[]为排序前序列,a[]为排序后序列*/
	int tmp=0;
	TYPE T=Insert;
	for (int i = 0; i < N - 1; i++) {
		if (a[i] > a[i + 1]) {
			tmp = i+1;//tmp指向无序的元素
			break;
		}
	}
	for (int i = tmp; i < N; i++) {
		if (b[i] != a[i]) {
			T = Heap;//如果后面部分不相同的话,是heapsort;循环正常结束的话就是insertsort;
			break;
		}
	}
	/*下一轮迭代*/
	if (T == Insert) {
		InsertSort(a, N, tmp);
	}
	if (T == Heap) {
		HeapSort(a, N);
	}

	return T;
}

void DispArr(int a[], int N) {
	for (int i = 0; i < N - 1; i++) {
		cout << a[i] << " ";
	}
	cout << a[N - 1];
}


int main() {
	int N;
	cin >> N;
	GetData(BeforeSort, N);
	GetData(AfterSort, N);
	TYPE IorH=InsertOrHeap(BeforeSort, AfterSort, N);
	if (IorH == Heap) {
		cout << "Heap Sort\n";
	}
	else {
		cout << "Insertion Sort\n";
	}
	DispArr(AfterSort, N);

	return 0;
}

测试结果

在这里插入图片描述

总结

总的来说跟前面一道题很像,采用观察的话来写会比较快,判断是插排和堆排挑容易的插排来判断,很简单。然后在判断出来的基础上进行下一次迭代。

插排的特点是前面的是有序的,而后面的部分AfterArr和BeforeArr是一样的。

堆排的特点是从最后一个元素开始进行排序,后面部分是有序的,并且前面的是无序的。堆排迭代的时候有一个整堆操作,总的来说比较简单。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值