1098 Insertion or Heap Sort (25 分) +测试要点

17 篇文章 0 订阅
10 篇文章 0 订阅

题目描述

1098 Insertion or Heap Sort (25 分)
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?
(……)

注意点

  1. 对于数组堆而言,编号是从1开始的(即,根节点编号为1而不为0)
  2. 注意是向上还是向下调整,调整的范围是什么
  3. 如果倒数第二个节点过不了,是因为数组大小的MAX不够大(堆根节点编号是从1开始的,堆数组的大小至少也得101),我这里开了110就过了
  4. 因为题目说不会出现插入排序和堆排序的中间结果相同的情况,所以可以直接排除最后一轮排序(本来就可以直接排除……都最后一轮了肯定结果一样),不需要对这个边界进行特殊的考虑和处理

通过代码

#include<iostream>
#include<fstream>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#define MAX 110
using namespace std;

int n, data[MAX], sorted[MAX], tmp[MAX];

int insertSort() {
    int flag;
    for(int i=1; i<=n; i++) {
        flag=1;
        for(int j=i+1; j>1; j--) {
            if(tmp[j-1]>tmp[j]) {
                swap(tmp[j], tmp[j-1]);
            } else {
                break;
            }
        }
        for(int j=1; j<=n; j++) {
            if(sorted[j]!=tmp[j]) {
                flag=0;
                break;
            }
        }
        if(flag==1) {
            if(i!=n-1) {
                i++;
                for(int j=i+1; j>0; j--) {
                    if(tmp[j]<tmp[j-1]) {
                        swap(tmp[j], tmp[j-1]);
                    } else {
                        break;
                    }
                }
            }
            return 1;
        }
    }
    return 0;
}

void downAdjust(int low, int high) {
    int i=low, j=i*2;
    while(j<=high) {
        if(j+1<=high && tmp[j+1]>tmp[j]) {
            j=j+1;
        }
        if(tmp[j]>tmp[i]) {
            swap(tmp[j], tmp[i]);
            i=j;
            j=i*2;
        } else {
            break;
        }
    }
}

void createHeap() {
    for(int i=n/2; i>0; i--) {
        downAdjust(i, n);
    }
}

void heapSort() {
    createHeap();
    for(int i=n; i>0; i--) {
        int flag=1;
        swap(tmp[i], tmp[1]);
        downAdjust(1, i-1);

        for(int j=1; j<=n; j++) {
            if(sorted[j]!=tmp[j]) {
                flag=0;
                break;
            }
        }
        if(flag==1) {
            if(i!=1) {
                swap(tmp[--i], tmp[1]);
                downAdjust(1, i-1);
            }
            return;
        }
    }
}

int main() {
//    fstream fin;
//    fin.open("tmp1.txt");
//
//    fin>>n;
//    for(int i=1; i<=n; i++) {
//        fin>>data[i];
//        tmp[i]=data[i];
//    }
//    for(int i=1; i<=n; i++) {
//        fin>>sorted[i];
//    }

    cin>>n;
    for(int i=1; i<=n; i++) {
        cin>>data[i];
        tmp[i]=data[i];
    }
    for(int i=1; i<=n; i++) {
        cin>>sorted[i];
    }
    if(insertSort()==1) {
        cout<<"Insertion Sort"<<endl;
    } else {
        for(int i=1; i<=n; i++) {
            tmp[i]=data[i];
        }
        heapSort();
        cout<<"Heap Sort"<<endl;
    }
    int i=1;
    for(; i<n; i++) {
        cout<<tmp[i]<<' ';
    }
    cout<<tmp[i];



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值