PAT A1098 insertion or heap sort (25)
来源:互联网 发布:ubuntu怎么启动selinux 编辑:程序博客网 时间:2024/05/29 18:30
和A1089类似
#include<cstdio>
#include<vector>#include<algorithm>
using namespace std;
const int MAX=105;
int n;
int init[MAX],parts[MAX],nexts[MAX],sorted[MAX];
int cmp(int a,int b){
return a<b;
}
int judge(){
int i,j,tag=0;
int temp;
for(i=2;i<=n;i++){//find i pos
tag=0;
for(j=i;j>1;j--){
if(sorted[j]<sorted[j-1]){
temp=sorted[j];
sorted[j]=sorted[j-1];
sorted[j-1]=temp;
}else break;
}
for(j=1;j<=n;j++){
if(sorted[j]!=parts[j]){
tag=1;
break;
}
}
if(tag==0) return i;
}
return -1;
}
void insertsort(int i){
int temp;
for(int j=i-1;j>=1;j--){
if(parts[j]>parts[j+1]){
temp=parts[j];
parts[j]=parts[j+1];
parts[j+1]=temp;
}else break;
}
for(int j=1;j<=n;j++){
if(j<n) printf("%d ",parts[j]);
else printf("%d\n",parts[j]);
}
}
int heap[102];
void downadjust(int low,int high){
int i=low;
int j=low*2;
while(j<=high){
if((j+1)<=high &&init[j+1]>init[j]){
j=j+1;
}
if(init[i]<init[j]){
swap(init[i],init[j]);
i=j;
j=i*2;
}else break;
}
}
int issame(){
int i,tag=0;
for(i=1;i<=n;i++){
if(init[i]!=parts[i]){
tag=1;
break;
}
}
if(tag==1) return -1;
else return 1;
}
void heapsort(){
int i;
for(i=(n/2);i>=1;i--){
downadjust(i,n);
}
for(i=n;i>1;i--){
swap(init[1],init[i]);
downadjust(1,i-1);
if(issame()==1) break;
}
i--;
swap(init[1],init[i]);
downadjust(1,i-1);
for(int j=1;j<=n;j++){
if(j<n) printf("%d ",init[j]);
else printf("%d\n",init[j]);
}
}
int main(){
scanf("%d",&n);
int i,temp;
for(i=1;i<=n;i++){
scanf("%d",&temp);
init[i]=temp;
sorted[i]=temp;
}
for(i=1;i<=n;i++){
scanf("%d",&temp);
parts[i]=temp;
}
int result=judge();
if(result!=-1){
printf("Insertion Sort\n");
insertsort(result+1);
}else{
printf("Heap Sort\n");
heapsort();
}
system("pause");
return 0;
}
0 0
- PAT A1098. Insertion or Heap Sort (25)
- PAT A1098 insertion or heap sort (25)
- A1098. 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)
- PAT 1098. Insertion or Heap Sort (25)
- PAT 1098-Insertion or Heap Sort (25)
- PAT-Insertion or Heap Sort
- PAT A 1098. Insertion or Heap Sort (25)
- PAT(Advance) 1098. Insertion or Heap Sort (25)
- PAT (Advanced Level) Practise 1098 Insertion or Heap Sort (25)
- PAT 1098 Insertion or Heap Sort (25)(待修改)
- 1098. Insertion or Heap Sort (25) PAT甲级
- [转]PAT甲级练习1098. Insertion or Heap Sort (25)
- PAT甲级1098. Insertion or Heap Sort (25)
- PAT-A-1098. Insertion or Heap Sort (25)
- PAT (Advanced Level) Practise 1098 Insertion or Heap Sort (25)
- OpenCV关于视频处理的学习(VideoCapture类)-在检测中一些必要的图像操作
- preprocess函数
- oj1045: 输入一个数插入有序数组中
- mysql的事务性,锁和隔离性
- 网络流24题5. 圆桌问题
- PAT A1098 insertion or heap sort (25)
- java读书笔记---垃圾回收
- 如果你想,那请去做
- 算法训练 打印乘法表
- python---map函数
- TCP/IP基础三
- HoG特征以及SVM的配合
- Linux学习之——rpm包相关操作
- UML用例图的作用、功能模块图作用与数据库设计三者关系