1098 Insertion or Heap Sort (25 分) (堆排序和插入排序的特征)1089 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.

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

题意:给一序列从小到大排序(可能是堆排序或插入排序),给出原序列和排序过程中的某一个过程,做出判断并输出该排序方法的下一步

特点:

  • 插入排序已经排过的序列从小到大,未排过的序列与原序列相同
  • 堆排序(大顶堆)每次将堆顶元素放在当前堆(数量每次减一)的末位,故从后往前看是从大到小的序列
  1. 根据插入排序的特点对序列作出判断从前往后(p=2)遍历找出第一个b[p-1]>b[p]的位置p,从该位置开始于原序列比较,若相同则为插入排序。插入排序进行下一步时,sort(b+1,b+1+p),再输出即可
  2. 若为堆排序,则从后往前找出第一个大于堆顶元素的值,下标为p,swap(b[p],b[1])然后向下调整,downAdjust(low,high),该堆的范围为low~high (即1到p-1) 

记忆:调换堆顶元素后的调整过程 

void downAdjust(int low,int high){
    int i=1,j=i*2;
    while(j<=high){
        if(j+1<=high&&b[j]<b[j+1]) j++;
        if(b[i]>b[j]) break;
        swap(b[i],b[j]);
        i=j,j=i*2;
    }
}
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int a[105],b[105];
void downAdjust(int low,int high){
	int i=1,j=i*2;
	while(j<=high){
		if(j+1<=high&&b[j]<b[j+1]) j++;
		if(b[j]<b[i]) break;
		swap(b[j],b[i]);
		i=j,j=i*2;
	}
}
int main() {
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) cin>>b[i];
	int p=2,q;
	while(p<=n&&b[p]>=b[p-1]) p++;
	q=p;
	while(q<=n&&b[q]==a[q]) q++;
	if(q==n+1){
		cout<<"Insertion Sort\n";
		sort(b+1,b+1+p);
	}else{
		cout<<"Heap Sort\n";
		p=n;
		while(p>=2&&b[p]>=b[1]) p--;
		swap(b[1],b[p]);
		downAdjust(1,p-1); 
	}
	cout<<b[1];
	for(int i=2;i<=n;i++) cout<<" "<<b[i];
	return 0;
}

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

插入排序特征明显(前几位递增,之后与未排序的序列相同),用来判断。之后直接排序下一步。

插入排序sort(a,a+i+2), i为相同的最后一位数字,下一步需要排i+1位数字,开区间是a+i+2

合并排序的话先还原到现在的序列,然后再排一步,代码如下:

int flag=1,k=1;
while(flag) {
    flag=0;
    for(i=0; i<n; i++) { 
        if(a[i]!=b[i]) 
            flag=1;   //没有还原到现有序列
    }
    k=k*2;//步长
    for(i=0; i<n/k; i++)
        sort(a+i*k,a+(i+1)*k); 
    sort(a+n/k*k,a+n); //剩余部分排序
}
若还原到原始序列,再排序一次即退出
#include<bits/stdc++.h>
using namespace std;
int a[105],b[105];
int main() {
	int n,method=0,i,j;
	scanf("%d",&n);
	for(i=0; i<n; i++)
		scanf("%d",&a[i]);
	for(i=0; i<n; i++)
		scanf("%d",&b[i]);
	for(i=0; i<n-1&&b[i]<=b[i+1]; i++);
	for(j=i+1; j<n&&b[j]==a[j]; j++);
	if(j==n) {
		printf("Insertion Sort\n");
		sort(a,a+i+2);
	} else {
		printf("Merge Sort\n");
		int flag=1,k=1;
		while(flag) {
			flag=0;
			for(i=0; i<n; i++) {
				if(a[i]!=b[i]) 
					flag=1;
			}
			k=k*2;
			for(i=0; i<n/k; i++)
				sort(a+i*k,a+(i+1)*k);
			sort(a+n/k*k,a+n);
		}
	}
	for(i=0; i<n; i++) {
		if(i) cout<<" ";
		cout<<a[i];
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值