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
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值