题目给你一个原数组,和一个经过排序之后的数组,但是只排到一半,没有排完,让你判断是堆排序还是插入排序,并且输出下一步排序之后的数组 。
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 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
int a[n];
int b[n];
int c[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
c[i] = a[i];
}
for (int i = 0; i < n; i++)
cin >> b[i];
int num = 0;
sort(c, c + n);
int cnt = 0;
while (b[cnt] <= b[cnt + 1])
{
cnt++;
}
if (cnt)
{
cout << "Insertion Sort" << endl;
sort(a, a + cnt + 2);
for (int i = 0; i < n; i++)
{
if (i != 0)
cout << " ";
cout << a[i];
}
}
else
{
cout << "Heap Sort" << endl;
int num = 0;
int r = 0;
while (num != n)
{
make_heap(a, a + n - r);
pop_heap(a, a + n - r);
num = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == b[i])
num++;
}
if (num == n)
{
r++;
make_heap(a, a + n - r);
pop_heap(a, a + n - r);
for (int i = 0; i < n; i++)
{
if (i != 0)
cout << " ";
cout << a[i];
}
}
r++;
}
}
system("pause");
return 0;
}