1089. Insert or Merge (25)
来源:互联网 发布:3799游戏盒软件 编辑:程序博客网 时间:2024/05/16 21:59
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.
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:103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0Sample Output 1:
Insertion Sort1 2 3 5 7 8 9 4 6 0Sample Input 2:
103 1 2 8 7 5 9 4 0 61 3 2 8 5 7 4 9 0 6Sample Output 2:
Merge Sort1 2 3 8 4 5 7 9 0 6
#include <stdio.h> #include <stdlib.h> void MergeSort(int b[], int end, int c[], int a[]){int i, LeftStart, LeftEnd, RightStart, RightEnd, next, j;for (i = 1; i < end; i = i * 2){for (LeftStart = 0; LeftStart < end - i; LeftStart = RightEnd){RightStart = LeftStart + i;LeftEnd = RightStart;RightEnd = LeftEnd + i;if (RightEnd > end){RightEnd = end;}next = 0;while (LeftStart < LeftEnd && RightStart < RightEnd){if (b[LeftStart] < b[RightStart]){a[next++] = b[LeftStart++];}else{a[next++] = b[RightStart++];}}while (LeftStart < LeftEnd){b[--RightStart] = b[--LeftEnd];}while (next > 0){b[--RightStart] = a[--next];}}for (j = 0; j < end; j++){if (b[j] != c[j]){break;}}if (j >= end){puts("Merge Sort");i = i * 2;for (LeftStart = 0; LeftStart < end - i; LeftStart = RightEnd){RightStart = LeftStart + i;LeftEnd = RightStart;RightEnd = LeftEnd + i;if (RightEnd > end){RightEnd = end;}next = 0;while (LeftStart < LeftEnd && RightStart < RightEnd){if (b[LeftStart] < b[RightStart]){a[next++] = b[LeftStart++];}else{a[next++] = b[RightStart++];}}while (LeftStart < LeftEnd){b[--RightStart] = b[--LeftEnd];}while (next > 0){b[--RightStart] = a[--next];}}for (j = 0; j < end - 1; j++){printf("%d ", b[j]);}printf("%d", b[j]);}}}void InsertSort(int a[], int end, int c[]){int i = 0, temp, j, k;for (i = 1; i < end; i++){temp = a[i];for (j = i; j > 0; j--){if (a[j - 1] > temp){a[j] = a[j - 1];}else{break;}}a[j] = temp;for (k = 0; k < end; k++){if (a[k] != c[k]){break;}}if (k >= end){puts("Insertion Sort");i++;temp = a[i];for (j = i - 1; j >= 0; j--){if (a[j] > temp){a[j + 1] = a[j];}else{break;}}a[j + 1] = temp;for (k = 0; k < end - 1; k++){printf("%d ", a[k]);}printf("%d", a[k]);return;}}}int main(){int a[110], b[110];int c[110];int i, N;scanf("%d", &N);for (i = 0; i < N; i++){scanf("%d", &a[i]);b[i] = a[i];}for (i = 0; i < N; i++){scanf("%d", &c[i]);}InsertSort(a, N, c);MergeSort(b, N, c, a);return 0;}
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- 1089. Insert or Merge (25)
- poj-3259-Wormholes
- 构造函数、拷贝构造函数和析构函数
- C++读取、写入配置文件
- Guice基本用法
- php-fallibility
- 1089. Insert or Merge (25)
- js实现瀑布流的两种方法
- nodejs
- tomcat出现Could not load the Tomcat server configuration at 。。异常解决办法
- HTTP协议的简单认识
- Linux多进程
- CodeFoeces732C 枚举
- 解题报告-PAT - Root of AVL Tree
- NSString之Category