PAT甲级真题 1098 Insertion or Heap Sort (25分) C++实现(插入排序、堆排序)

news/2024/5/19 5:47: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?
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, 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.
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 resulting 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

思路

类似于PAT甲级真题 1089 Insert or Merge (25分)

1. 判定是否为插入排序

记两个数组分别为a、b。

首先定位数组b中当前未排序元素位置k,若a、b后面的元素相同:

if (equal(a.begin()+k, a.end(), b.begin()+k))

则可判定为Insertion Sort。

用到了algorithm.h中的equal函数,定义是:

bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);

2. 对于插入排序

直接调用sort函数,将下一位纳入已排序范围即可。

3. 对于堆排序

首先找出当前未排序位置边界:

        k = n - 1;
        while (k>0 && b[k]>=b[0]) k--;  //未排序位置边界

当前堆顶是未排序元素的最大值,已排序元素都是比该值还要大的。

然后再进行一次堆排序,步骤是:

  1. 将堆顶与最后一个元素交换,接入有序区;
  2. 重新建堆。从根节点向下,将父节点与子节点中较大者交换,交换后递归处理受影响的子树。重点要掌握sift()建堆函数。

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void sift(vector<int> &b, int i, int n){
    int l = 2 * i + 1;
    int r = 2 * i + 2;
    if (l >= n) return;  //到达叶子
    int maxI = i;  //记录i、l、r中最大值位置
    if (l<n && b[l]>b[maxI]) maxI = l;
    if (r<n && b[r]>b[maxI]) maxI = r;
    if (maxI != i) {
        swap(b[i], b[maxI]);
        sift(b, maxI, n);
    }
}

int main(){
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i=0; i<n; i++){
        cin >> a[i];
    }
    for (int i=0; i<n; i++){
        cin >> b[i];
    }

    int k = 1;  //首个逆序位置
    while (k<n && b[k-1]<=b[k]) k++;
    //b剩下的逆序部分是否与a后面部分相同,若相同就是Insertion Sort
    if (equal(a.begin()+k, a.end(), b.begin()+k)){
        cout << "Insertion Sort" << endl;
        sort(b.begin(), b.begin()+k+1);
    } 
    else{
        cout << "Heap Sort"  << endl;
        k = n - 1;
        while (k>0 && b[k]>=b[0]) k--;  //未排序位置边界
        swap(b[0], b[k]);  //将当前最大值放到排序区
        sift(b, 0, k); 
    }
    cout << b[0];
    for (int i=1; i<n; i++){
        cout << " " << b[i];
    }
    return 0;
}


http://www.niftyadmin.cn/n/905640.html

相关文章

计算机电子表格考点,计算机一级考试历年常见考点总结:Excel电子表格

Excel电子表格(一定要保存正确)1、插入行、列;设置行高、列宽&#xff0c;最适合行高、最适合列宽的设置;调整行或列的顺序(剪切/粘贴);2、合并及居中&#xff0c;注意不要整行合并;3、设置单元格格式&#xff1a;数字、对齐、字体、边框、底纹(图案)格式;4、数据填充&#xff…

链表问题(3)-----反转

1、题目&#xff1a;反转单链表或双链表 要求&#xff1a;如果链表长度为N&#xff0c;时间复杂度为O&#xff08;N&#xff09;&#xff0c;额外的空间复杂度为O&#xff08;1&#xff09; 反转单链表的思路&#xff1a; 1 → 2 → 3 → 4 → 5 &#xff08;1&#xff09;firs…

PAT甲级真题 1099 Build A Binary Search Tree (30分) C++实现 (二叉搜索树BST inorder中序遍历得到有序数列)

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greate…

荒野行动为什么不能获取服务器信息,荒野行动获取服务器信息一直不动 服务器信息0解决方法...

荒野行动获取服务器信息一直不动怎么办呢?荒野行动今天给玩家带来了全新的更新&#xff0c;也给玩家带来了全新的50VS50的模式&#xff0c;很多玩家都想第一时间进入游戏中体验一番啊&#xff0c;但是为什么会卡在获取服务器信息的界面呢?这是为什么呐?很多玩家都想知道怎么…

C#中SetWindowPos函数详解

[DllImport("user32.dll")]private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);SetWindowPos( hWnd: HWND; {窗口句柄} hWndInsertAfter: HWND; {窗口的 Z 顺序} X, Y: Integer; {位置} cx, …

哪个虚拟服务器免费,免费虚拟主机空间选择哪家会更好?

虚拟主机在使用过程中需要提供比较大的空间&#xff0c;因为如果内存不足的话&#xff0c;会出现网站访问速度比较慢&#xff0c;或者直接卡顿和卡死的情况&#xff0c;当然现在市场中也有免费虚拟主机空间&#xff0c;大家在选择的时候就可以看看选择哪一家的虚拟主机空间效果…

堆排序heapSort C++实现

#include <iostream> using namespace std; // 对arr[i]为根的子树建堆&#xff1b;i&#xff1a;根节点下标 n&#xff1a;堆大小 void heapify(int arr[], int n, int i) { int largest i; // Initialize largest as root int l 2*i 1; // left 2*i 1 int r 2*…

systick运用

systick的原理前一篇博文有介绍&#xff0c;简而言之就是SysTick定时器是一个24位的倒计数&#xff0c;当倒计数为0时&#xff0c;将从RELOAD寄存器中取值作为定时器的初始值&#xff0c;同时可以选择在这个时候产生中断(异常号:15)。例如从RELOAD的值为999&#xff0c;那么当倒…