09-排序2 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 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
Note1
- 测试点2 4 5 6 都没过,自己的思路, 比较麻烦
Code1
#include<iostream>
using namespace std;
#define MAX 110
int a[MAX], b[MAX], c[MAX];
void Merge(int a[], int low, int mid, int high){
for(int i = low; i <= high; i++ ){
b[i] = a[i];
}
int t1 = low, t2 = mid + 1, cnt = low;
for(;t1 <= mid && t2 <= high; cnt++ ){
if(b[t1] < b[t2]) a[cnt] = b[t1++];
else a[cnt] = b[t2++];
}
while(t1 <= mid) a[cnt++] = b[t1++];
while(t2 <= high) a[cnt++] = b[t2++];
}
// void Mergesort(int a[], int low,int high){
// if(low < high){
// int mid = (low + high) / 2;
// Mergesort(a, low, mid);
// Mergesort(a, low + 1, high);
// Merge(a, low, mid, high);
// }
// }
int main(){
int num;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
}
int last = -1, flag = -1;
for( int i = 0; i < num; i++){
cin >> c[i];
if(last > c[i] && flag == -1) flag = i;
last = c[i];
}
int i;
for(i = flag; i < num; i++){
if(a[i] != c[i]){ cout << "Merge Sort" << endl; break;}
}
if( i == num ){
cout << "Insertion Sort" << endl;
int temp = a[flag], j;
for(j = flag - 1; j > 0; j--){
if(c[j] > temp) c[j + 1] = c[j];
else break;
}
c[j + 1] = temp;
cout << c[0];
for(j = 1; j < num; j++){
cout << " " << c [j];
}
}
else{
int l, layer = 0;
for(l = 2; l <= num; l *= 2){ //有序子列的长度
for(int j = 0; j * l + l < num; j += 2){ //有序子列之间的元素
if(c[l - 1 + j * l] > c[l + j * l]) { layer = l; break;}
}
if(layer > 0) break;
}
for(int i = 0; 2 * layer + i - 1 < num; i += 2 * layer){
Merge(c, i, layer + i - 1, 2 * layer + i - 1);
}
cout << c[0];
for(int j = 1; j < num; j++){
cout << " " << c[j];
}
}
return 0;
}
Note2
- 充分利用stl的sort函数 AC
- 归并排序注意处理最后剩下的一段,卡了好久
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 110
int a[MAX], b[MAX], c[MAX];
void Merge(int a[], int low, int mid, int high){
for(int i = low; i <= high; i++ ){
b[i] = a[i];
}
int t1 = low, t2 = mid + 1, cnt = low;
for(;t1 <= mid && t2 <= high; cnt++ ){
if(b[t1] < b[t2]) a[cnt] = b[t1++];
else a[cnt] = b[t2++];
}
while(t1 <= mid) a[cnt++] = b[t1++];
while(t2 <= high) a[cnt++] = b[t2++];
}
// void Mergesort(int a[], int low,int high){
// if(low < high){
// int mid = (low + high) / 2;
// Mergesort(a, low, mid);
// Mergesort(a, low + 1, high);
// Merge(a, low, mid, high);
// }
// }
int main(){
int num;
cin >> num;
for(int i = 0; i < num; i++){
cin >> a[i];
}
int last = -1, flag = -1;
for( int i = 0; i < num; i++){
cin >> c[i];
if(last > c[i] && flag == -1) flag = i;
last = c[i];
}
int i;
for(i = flag; i < num; i++){
if(a[i] != c[i]){ cout << "Merge Sort" << endl; break;}
}
if( i == num ){
cout << "Insertion Sort" << endl;
sort(c, c + flag +1);
// int temp = a[flag], j;
// for(j = flag - 1; j > 0; j--){
// if(c[j] > temp) c[j + 1] = c[j];
// else break;
// }
// c[j + 1] = temp;
cout << c[0];
for(int j = 1; j < num; j++){
cout << " " << c [j];
}
}
else{
int l, layer = 0;
for(l = 2; l <= num; l *= 2){ //有序子列的长度
for(int j = 0; j * l + l < num; j += 2){ //有序子列之间的元素
if(c[l - 1 + j * l] > c[l + j * l]) { layer = l; break;}
}
if(layer > 0) break;
}
int i;
for( i = 0; 2 * layer + i - 1 < num; i += 2 * layer){
Merge(c, i, layer + i - 1, 2 * layer + i - 1);
}
sort(c + i, c + num);
cout << c[0];
for(int j = 1; j < num; j++){
cout << " " << c[j];
}
}
return 0;
}
Code3
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, i, count, length, a[100], b[100];
int flag = 1;
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 = 1; b[i] >= b[i - 1]; i++);
count = i;
for (;i < n && a[i] == b[i];i++);
if (i == n) {
printf("Insertion Sort\n");
std::sort(b, b + count + 1);
printf("%d", b[0]);
for (i = 1;i < n;i++)
printf(" %d", b[i]);
}
else {
printf("Merge Sort\n");
for (length = 2;flag;length *= 2) {
for (i = length; i < n; i += length * 2) {
if (b[i - 1] > b[i]) {
flag = 0;break;
}
}
}
for (i = 0;i + length <= n;i += length)
sort(b + i, b + i + length);
sort(b + i, b + n);
printf("%d", b[0]);
for (i = 1;i < n;i++)
printf(" %d", b[i]);
}
return 0;
}