mirror of
https://github.com/hao14293/2021-Postgraduate-408.git
synced 2026-02-02 18:20:30 +08:00
Create 09-排序2 Insert or Merge (25 分).cpp
This commit is contained in:
41
ZJU_MOOC_数据结构/09-排序2 Insert or Merge (25 分).cpp
Normal file
41
ZJU_MOOC_数据结构/09-排序2 Insert or Merge (25 分).cpp
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user