和前一题差不多,把归并排序换成了堆排序。要点还是每一次排序进行判断

开始犯了个错误 堆排序该用origin2 结果一直在排序origin ,误导了半天以为是逻辑错误。。。一直在检查逻辑

建立最大堆

排序并调整下滤

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?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤100). Then in the next line, NN 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.

Output Specification:

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.

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 <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; typedef int ElementType;
bool Judge(int origin[], int changed[], int len)
{
for(int i = ; i < len; i++) {
if(origin[i] != changed[i])
return false;
}
return true;
} //插入排序 (需排序数组,数组长度,排序次数)
void isInsertionSort(ElementType origin[], int N, int times)
{
int i;
ElementType temp = origin[times]; //取出未排序序列中的第一个元素
for (i = times; i > && origin[i-] > temp; i-- )
origin[i] = origin[i-]; //依次与已排序序列中元素比较并右移
origin[i] = temp;
} void Swap( ElementType *a, ElementType *b )
{
ElementType t = *a;
*a = *b;
*b = t;
}
/* 改编PercDown( MaxHeap H, int p )*/
void PercDown( ElementType A[], int p, int N )
{
/* 将N个元素的数组中以A[p]为根的子堆调整为最大堆 */
int Parent, Child; ElementType X = A[p]; /* 取出根结点存放的值 */
for( Parent=p; (Parent*+) < N; Parent=Child ) {
Child = Parent * + ;
if( (Child != N-) && (A[Child] < A[Child+]) )
Child++; /* Child指向左右子结点的较大者 */
if( X >= A[Child] ) break; /* 找到了合适位置 */
else /* 下滤X */
A[Parent] = A[Child];
}
A[Parent] = X;
} void HeapSort(ElementType A[], int N, int changed[])
{
for(int i = N/-; i >= ; i--) /* 建立最大堆 */
PercDown( A, i, N ); for(int i = N-; i > ; i--) {
/* 删除最大堆顶 */
Swap(&A[], &A[i] );
PercDown(A, , i); if( Judge(A, changed, N) ) { //是堆排序
printf("Heap Sort\n"); Swap(&A[], &A[i-] ); //在执行一次堆排序
PercDown(A, , i-);
for(int j = ; j < N-; j++)
printf("%d ",A[j]);
printf("%d\n",A[N-]);
return;
}
}
} int main()
{
int N;
int origin[],origin2[],changed[];
scanf("%d", &N);
for(int i = ; i < N; i++) { //origin origin1 初始序列
scanf("%d",&origin[i]);
origin2[i] = origin[i];
}
for(int i = ; i < N; i++) //changed 排序后序列
scanf("%d",&changed[i]); for(int i = ; i < N; i++) {
isInsertionSort(origin, N, i);
if( Judge(origin, changed,N) ) { //是插入排序
printf("Insertion Sort\n");
isInsertionSort(origin, N, i+);
for(int j = ; j < N-; j++)
printf("%d ",origin[j]);
printf("%d\n",origin[N-]);
return ;
}
} HeapSort(origin2,N,changed);
return ;
}
 

09-排序3 Insertion or Heap Sort的更多相关文章

  1. PAT甲级1098. Insertion or Heap Sort

    PAT甲级1098. Insertion or Heap Sort 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.在每次迭代中,插入排序从输入数据中删除一个元素 ...

  2. PAT甲级——1098 Insertion or Heap Sort (插入排序、堆排序)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90941941 1098 Insertion or Heap So ...

  3. pat1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  4. pat 甲级 1098. Insertion or Heap Sort (25)

    1098. Insertion or Heap Sort (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  5. PAT_A1098#Insertion or Heap Sort

    Source: PAT_A1098 Insertion or Heap Sort (25 分) Description: According to Wikipedia: Insertion sort  ...

  6. 1098 Insertion or Heap Sort——PAT甲级真题

    1098 Insertion or Heap Sort According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  7. PTA Insertion or Heap Sort

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  8. 1098 Insertion or Heap Sort

    1098 Insertion or Heap Sort (25 分) According to Wikipedia: Insertion sort iterates, consuming one in ...

  9. PTA 09-排序3 Insertion or Heap Sort (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/676 5-14 Insertion or Heap Sort   (25分) Accor ...

  10. Insertion or Heap Sort

    7-14 Insertion or Heap Sort(25 分) According to Wikipedia: Insertion sort iterates, consuming one inp ...

随机推荐

  1. [系统] 安装Ubuntu 双系统 - 失败

    因为工作原因, 所以需要装ubuntu系统. 在网络上查了一下, 一般都是使用U盘安装. 但是由于手头上既没有U盘又没有光盘,只能用硬盘安装了. 查一下, 使用wubi安装方式从硬盘安装, 非常方便. ...

  2. imp-00002 无法打开。。

    路径错了无疑 文件名少了个字符无疑 文件名错了无疑

  3. 默菲定律 [Murphy's Law]

    一.关于默菲定律(Murphy's Law)   “墨菲定律”.“帕金森定律”和“彼德原理”并称为二十世纪西方文化三大发现. “墨菲定律”的原话是这样说的:If there are two or mo ...

  4. 多XML追加操作

    假设要统计当前系统中所有的试卷进行分析,试卷是以XML格式存储的,所有这就需要将所有零散的XML文件整合起来,处理成一个完整的XML文件,进行分析, 下面是简单额处理方法: 当前XML文件格式: &l ...

  5. Linux下搭建Lotus Domino集群

    Linux下搭建Lotus Domino 集群 本文内容是Linux平台下Lotus Domino服务器部署案例(http://chenguang.blog.51cto.com/350944/1334 ...

  6. phonegap android3.5.1 Crosswalk

    1. your phonegap platform for android update 3.5.1 cordova platform add android@3.5 2. download cros ...

  7. CLRS:build_max_heap(strorage in array)

    //用满二叉树存储,从n/2处开始递归向上调整(n/2后均为叶子节点,无需调整)使得根最大 //满二叉树顺序存储,左子2i,右子2i+1: #include<stdio.h>#includ ...

  8. 使用fiddler2抓取手机发出的请求信息

    fiddler2 简介:抓包软件,可以替换服务器js,从而实现本地调试 初始化设置: 1.工具——fiddler选项——常规——允许远程计算机连接(打钩)     2.按下图设置   3.设置连接,如 ...

  9. jQ复制按钮的插件zclip

    zclip官网:http://www.steamdev.com/zclip/ swf文件国内下载:ZeroClipboard.swf jQuery-zclip是一个复制内容到剪贴板的jQuery插件, ...

  10. libpcap报文解析: ipv4、ipv6 @ 2014.7.2

    #include <string.h> #include <stdlib.h> #include <pcap.h> #include <stdio.h> ...