A1089. 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 (<=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 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 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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void merge(int num[], int low1, int high1, int low2, int high2){
int temp[];
int index = , i = low1, j = low2;
while(i <= high1 && j <= high2){
if(num[i] < num[j])
temp[index++] = num[i++];
else
temp[index++] = num[j++];
}
while(i <= high1)
temp[index++] = num[i++];
while(j <= high2)
temp[index++] = num[j++];
for(int k = ; k < index; k++)
num[low1 + k] = temp[k];
}
int comp(int num1[], int num2[], int len){
for(int i = ; i < len; i++){
if(num1[i] != num2[i])
return ;
}
return ;
}
void mergeSort(int num[], int num2[], int len){
int isSame = , show = ;
for(int step = ; step / <= len; step *= ){
for(int i = ; i < len; i += step){
int mid = i + step / - ;
merge(num, i, mid, mid + , min(i + step - , len - ));
}
isSame = comp(num, num2, len);
if(isSame == ){
show = ;
continue;
}
if(show == ){
printf("Merge Sort\n");
printf("%d", num[]);
for(int i = ; i < len; i++)
printf(" %d", num[i]);
return;
}
}
}
void inser(int num[], int num2[], int len){
int isSame = , show = ;
for(int i = ; i < len; i++){
int temp = num[i];
int j = i - ;
while(j >= && temp < num[j]){
num[j + ] = num[j];
j--;
}
num[j + ] = temp;
isSame = comp(num, num2, len);
if(isSame == ){
show = ;
continue;
}
if(show == ){
printf("Insertion Sort\n");
printf("%d", num[]);
for(int i = ; i < len; i++)
printf(" %d", num[i]);
return;
}
}
}
int main(){
int N, num1[], num1_[], num2[];
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &num1[i]);
num1_[i] = num1[i];
}
for(int i = ; i < N; i++){
scanf("%d", &num2[i]);
}
mergeSort(num1, num2, N);
inser(num1_, num2, N);
cin >> N;
return ;
}
总结:
1、归并排序, 合并函数:
void merge(int num[], int low1, int high1, int low2, int high2){
int temp[];
int index = , i = low1, j = low2;
while(i <= high1 && j <= high2){
if(num[i] < num[j])
temp[index++] = num[i++];
else
temp[index++] = num[j++];
}
while(i <= high1)
temp[index++] = num[i++];
while(j <= high2)
temp[index++] = num[j++];
for(int k = ; k < index; k++)
num[low1 + k] = temp[k];
}
非递归写法:
void mergeSort(int num[], int len){
for(int step = ; step <= len; step *= ){ //初始步长为2, 逐步变为4、8、16
for(int i = ; i < len; i += step){ //i + step为每个子区间的首部
int mid = i + step / - ;
merge(num, i, mid, mid + , min(i + step - , len - ));
}
}
}
2、插入排序:将a[0]至a[i]视作有序序列,将a[i + 1]插入a[0]至a[i]中,使之有序。
void inser(int num[], int len){
for(int i = ; i < len; i++){
int temp = num[i];
int j = i - ;
while(j >= && temp < num[j]){
num[j + ] = num[j];
j--;
}
num[j + ] = temp;
}
}
3、注意,排序用的num数组需要两个,归并排序之后num数组已经改变,不可再用于插入排序。
A1089. Insert or Merge的更多相关文章
- A1089 Insert or Merge (25 分)
一.技术总结 看到是一个two pointers问题,核心是要理解插入排序和归并排序的实现原理,然后判断最后实现 可以知道a数组和b数组怎么样判断是插入排序还是归并排序,因为插入排序是来一个排一个,所 ...
- PAT甲级——A1089 Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT_A1089#Insert or Merge
Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...
- PTA Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 60. Insert Interval && Merge Intervals
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- PAT1089. Insert or Merge
PAT1089. Insert or Merge 题目大意 给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代 ...
随机推荐
- BugkuCTF web基础$_GET
前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...
- MongoDB集群运维笔记
前面的文章介绍了MongoDB副本集和分片集群的做法,下面对MongoDB集群的日常维护操作进行小总结: MongDB副本集故障转移功能得益于它的选举机制.选举机制采用了Bully算法,可以很方便从分 ...
- Linux下绑定网卡的操作记录
公司采购的服务器安装了双网卡,并进行bond网卡绑定设置,网卡绑定mode共有七种(0~6) bond0.bond1.bond2.bond3.bond4.bond5.bond6. 第一种模式:mod= ...
- Tomcat通过Memcached实现session共享的完整部署记录
对于web应用集群的技术实现而言,最大的难点就是:如何能在集群中的多个节点之间保持数据的一致性,会话(Session)信息是这些数据中最重要的一块.要实现这一点, 大体上有两种方式:一种是把所有Ses ...
- Linux下monit进程管理操作梳理
Monit对运维人员来说可谓神器,它是一款功能非常丰富的进程.文件.目录和设备的监测工具,用于Unix平台.它可以自动修复那些已经停止运作的程序,特使适合处理那些由于多种原因导致的软件错误.Monit ...
- android studio报Resolved versions for app (26.1.0) and test app (27.1.1)differ. 错误的解决办法
https://blog.csdn.net/qq_36636969/article/details/80278150
- swap函数
#include<iostream> using namespace std; void swap(int& a,int& b){ int t=a; a=b; b=t; } ...
- 如何在java项目中使用lucene
lucene是一个开源的全文检索引擎工具包,但它不是一个成型的搜索引擎,它的功能就是负责将文本数据按照某种分词算法进行分词,分词后的结果存储在索引库中,然后根据关键字从索引库检检索. 那么应该如何使用 ...
- week3个人作业
一.必应词典的bug 必应词典占用资源过多,作为后台软件,必应词典的内存占用是其他的四五倍 适应能力弱,经常与其他软件冲突,兼容性差 二.分析 根据我的分析,团队人数6人左右,计算机大学毕业生,并有专 ...
- 个人博客作业Week3--必应词典案例分析
第一部分 调研,评测 (软件的bug,功能评测,黑箱测试,第8章 用户调研,12 章软件的用户体验) 下载并使用,按照描述的bug定义,找出几个功能性的比较严重的bug.至少两个.用专业的语言描述( ...