首页 > 其他分享 >Insertion or Heap Sort (25)

Insertion or Heap Sort (25)

时间:2022-09-19 02:22:05浏览次数:58  
标签:Sort 25 head int temporal initial arr Heap sorted

题目描述

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.

输入例子:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

 

输出例子:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

该题题意简明,我用的方法相对暴力,直接模拟两种排序进行一一比对。

堆排序时,大根堆的建立是自底向上,之后的调整是自顶向下,明白了这一点调整函数就比较好写了

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int initial[105],sorted[105];
 4 void adjust(int *arr,int head,int tail){ //自head往tail调整
 5     int temporal_max; //子树中更大的一棵
 6     while(head*2<=tail){
 7         if (head*2<tail){//该父节点有两棵子树
 8             temporal_max=arr[head*2]>arr[head*2+1]?head*2:head*2+1;
 9         }
10         else{ //该父节点只有一棵子树
11             temporal_max=head*2;
12         }
13         if (arr[temporal_max]>arr[head]){
14             int temp=arr[temporal_max];
15             arr[temporal_max]=arr[head];
16             arr[head]=temp;
17         }
18         else{
19             break;
20         }
21         head=temporal_max;
22     }
23 }
24 
25 int main(){
26     int n;
27     cin>>n;
28     memset(initial,0,sizeof(initial));
29     memset(sorted,0,sizeof(sorted));
30     for(int i=1;i<=n;++i){
31         cin>>initial[i];//堆排序为了方便计算下标从1开始
32     }
33     for(int i=1;i<=n;++i){
34         cin>>sorted[i];
35     }
36     //判断是否为插入排序
37     int flag;
38     for(int i=1;i<=n;++i){
39         sort(initial+1,initial+i+1);
40         flag=1;
41         for (int j=1;j<=n;++j){
42             if (initial[j]!=sorted[j]){
43                 flag=0;
44                 break;
45             }
46         }
47         if (flag==1){ //判定为插入排序
48             cout<<"Insertion Sort"<<endl;
49             sort(initial+1,initial+i+2);
50             for (int k=1;k<=n;++k){
51                 cout<<initial[k];
52                 if (k!=n)
53                     cout<<" ";
54             }
55             break;
56         }
57     }
58     if (flag==0){ //判定为堆排序
59         cout<<"Heap Sort"<<endl;
60         sort(initial+1,initial+n+1);
61         int index;
62         for (int i=n;i>0;--i){
63             if (initial[i]!=sorted[i]){
64                 index=i; //定位到当前趟数
65                 break;
66             }
67         }
68         //下一趟堆排序
69         int temp=sorted[1];
70         sorted[1]=sorted[index];
71         sorted[index]=temp;
72         adjust(sorted,1,index-1);
73         for (int i=1;i<=n;++i){
74             cout<<sorted[i];
75             if (i!=n){
76                 cout<<" ";
77             }
78         }
79     }
80 }

 

标签:Sort,25,head,int,temporal,initial,arr,Heap,sorted
From: https://www.cnblogs.com/coderhrz/p/16706433.html

相关文章

  • 2022-2023-1 20221325 《计算机基础与程序设计》第三周学习总结
    作业信息班级链接:https://edu.cnblogs.com/campus/besti/2022-2023-1-CFAP作业要求:https://www.cnblogs.com/rocedu/p/9577842.html#JXJC作业目标:数字分类与计数法位......
  • ili9325屏幕横屏竖屏方向
    首先这个屏幕默认就是竖着的,他的240方向排列着子像素 是行扫描方向 有三个寄存器会影响显示的扫描顺序//R1是SS:选择源极驱动器输出的移位方向。这里以竖屏,......
  • 第 25 题:浏览器和 Node 事件循环的区别
    先上链接:浏览器与Node的事件循环(EventLoop)有何区别?html#event-loopsNode.js事件循环,定时器和process.nextTick()第一个链接里面大佬讲的已经非常透彻了我来总......
  • [Go] Stack and Heap, Garbage Collections
    StackisdedicatedtofunctionLocalvariablesarestoreinstackDeallocatedafterfunctioncompletesHeapispersistenCarbageCollectionsGoisacompi......
  • DAY 252 Python定时任务
    在日常工作中,我们常常会用到需要周期性执行的任务,一种方式是采用Linux系统自带的crond结合命令行实现。另外一种方式是直接使用Python。接下来整理的是常见的Python定......
  • Vue sortable实现排序功能
     1.vue代码 <template><el-table@selection-change="handleSelectionChange"@sort-change="sortChange"v-loading="loading"id="TableColumnID"eleme......
  • PAT Advanced 1032 Sharing(25)
    题目描述:TostoreEnglishwords,onemethodistouselinkedlistsandstoreawordletterbyletter.Tosavesomespace,wemayletthewordssharethesames......
  • 2022-9-6 #25 None
    这是否也算一种闲话。受到音游的影响,最近一个月都没咋做题,也没更博。我的评价是......
  • CF1325F Ehab's Last Theorem
    传送门思路dfs树的一道出色的应用题令\(k=\lceil\sqrtn\rceil\)我们先按照遍历的顺序构建出dfs树对于一条返祖边\((u,v)\),如果有\(dep_u-dep_v+1\gek\),......
  • Python 中的 sorted 和 sort的区别
    Python中的sorted和sort的区别#sort与sorted区别:#sorted()是内置函数.sorted可以对所有可迭代的对象进行排序操作,有返回值,返回列表;#sort是list上的方法,是对......