Skip to content

Commit

Permalink
Create 1089 Insert or Merge (25 εˆ†).cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
hao14293 authored May 12, 2019
1 parent 4584822 commit bee1b68
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions PAT/PAT-A/CPP/1089 Insert or Merge (25 εˆ†).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
int *a = new int [n];
int *b = new int [n];
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
int i, j;
for (i = 0; i < n - 1 && b[i] <= b[i + 1]; i++);
for (j = i + 1; a[j] == b[j] && j < n; j++);
if (j == n) {
cout << "Insertion Sort" << endl;
sort(a, a + i + 2);
} else {
cout << "Merge Sort" << endl;
int k = 1, flag = 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 (j = 0; j < n; j++) {
if (j != 0) printf(" ");
printf("%d", a[j]);
}
delete [] a;
delete [] b;
return 0;
}

0 comments on commit bee1b68

Please sign in to comment.