Insertion or Heap Sort (25)
来源:互联网 发布:scancard文字识别软件 编辑:程序博客网 时间:2024/06/05 18:32
题目描述
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.Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than a linear-time search to find the maximum.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?
输入描述:
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.
输出描述:
For each test case, print in the first line either "Insertion Sort" or "Heap 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.
输入例子:
103 1 2 8 7 5 9 4 6 01 2 3 7 8 5 9 4 6 0
输出例子:
Insertion Sort1 2 3 5 7 8 9 4 6 0
#include <iostream>
#include<vector>
using namespace std;
vector<
int
> a,b;
int
n,t;
int
insert_sort(vector<
int
> x){
int
t2=
0
;
for
(
int
i=
1
;i<n;i++)
{
int
temp=
1
,k=
0
;
int
t=x[i],j=i;
while
(j>
0
&&t<x[j-
1
])
x[j]=x[j-
1
],j--;
x[j]=t;
if
(t2) {
cout<<
"Insertion Sort"
<<endl;
cout<<x[
0
];
for
(
int
k=
1
;k<n;k++)
cout<<
" "
<<x[k];
return
1
;
}
for
(
int
k=
0
;k<n;k++)
{
if
(x[k]!=b[k]) {temp=
0
;
break
;}
}
if
(temp) t2=
1
;
}
return
0
;
}
void
adjust(
int
i,
int
n,vector<
int
>&x){
int
t=
0
;
if
(
2
*i+
1
<n){
if
(
2
*i+
2
<n) t=x[
2
*i+
1
]>x[
2
*i+
2
]?
2
*i+
1
:
2
*i+
2
;
else
t=
2
*i+
1
;
if
(x[t]>x[i])
swap(x[t],x[i]),adjust(t,n,x);
}
}
int
heap_sort(vector<
int
> x){
int
t2=
0
;
for
(
int
i=n/
2
;i>=
0
;i--)
{
adjust(i,n,x);
}
for
(
int
i=n-
1
;i>=
0
;i--)
{
/*
for(int k=0;k<n;k++)
cout<<x[k]<<" ";
cout<<endl;
*/
int
temp=
1
;
swap(x[i],x[
0
]);
adjust(
0
,i,x);
if
(t2) {
cout<<
"Heap Sort"
<<endl;
cout<<x[
0
];
for
(
int
k=
1
;k<n;k++)
cout<<
" "
<<x[k];
return
1
;
}
for
(
int
k=
0
;k<n;k++)
{
if
(x[k]!=b[k]) {temp=
0
;
break
;}
}
if
(temp) t2=
1
;
}
return
0
;
}
int
main(){
cin>>n;
for
(
int
i=
0
;i<n;i++)
{cin>>t;
a.push_back(t);
}
for
(
int
i=
0
;i<n;i++)
{cin>>t;
b.push_back(t);
}
if
(!insert_sort(a))
heap_sort(a);
return
0
;
}
0 0
- Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- PAT 1098. Insertion or Heap Sort (25)
- [PAT]1098. Insertion or Heap Sort (25)
- 【PAT】1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 1098. Insertion or Heap Sort (25)
- 自动生成与数据库交互代码
- DrawerLayout——侧滑
- 第2周项目0 宣告主权
- c++容器使用50条总结
- Oracle Number型的深入理解
- Insertion or Heap Sort (25)
- JavaScript字符串与数字的特殊处理
- MongoDB基本命令用
- 仿Mars MP3播放器项目1
- [Editor][Emacs] 002从零到高级的进阶 - 实践开发 - emacs tutorial的使用和基本导航键
- Spring 的高级依赖关系配置
- Android 集成高德地图
- SQL Server 方括号的作用
- freemarker 数字显示问题