PAT Advanced 1098. Insertion or Heap Sort in C.

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At 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 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


题目的意思就是,给一个原始序列和经过若干步的排序算法之后的序列,然后让你来判断是插入排序还是堆排序。
下面简述下两个排序的原理和特点(针对线性表和递增排序):

  1. 插入排序:
    首先分成有序(在前)和无序(在后)部分,每次从无序部分中提出一个元素,然后查找其在有序部分中的位置,插入。迭代至全部有序为止。特点->前面有序,后面无序,且与原始序列相同(我脑子里没找出其他表述,捂脸)
  2. 堆排序:
    同样是分成有序(在后)和无序(在前)两部分,只是这里无序的部分是大根堆,每次将大根堆的根元素放入有序部分,直至全部有序为止。特点->前面无序,但是无序部分的最大值在首位(当然还要满足最大堆特点),后面无序,且无序部分的最小值在首位,且比无序部分最大值大。

所有抓住两者的特点和区别是本题的解题关键。

下面是我的思路:先判断是否是插入排序(前半部分有序,且无序部分与原始序列相同)->如果不是,则就是堆排序(之所以先判断插入,我觉得插入排序的判断条件对于未知数列,能包含更多的情况,而且堆的条件比较严格,而且对于未知数列不好判断,自己理解有限:第一个元素是无序部分的最大值,有序部分的最小值比其大,我在解题过程中尝试过先判断堆,否则是插入,但是有几个测试点一直过不了,可能是自己想的不够周到。[1-25-2018 to be modified])-> 然后进行相应的下一步即可。


#include <stdio.h>
#define maxNum 101
void adjustDown(int a[],int length)
{
    /*heap sort's adjust down*/
    int i=0,temp,next;
    /*swap first and 'last' elements*/
    temp=a[length];
    a[length]=a[0];
    a[0]=temp;
    /*end swapping*/

    temp=a[0];
    while(i<length)
    {
        next=2*i+1;
        if(next>=length) break; //important!!!!!
        if(next+1<length&&a[next]<a[next+1])
        {
            /* the bigger child*/
            next++;
        }
        if(temp>=a[next]) break; /*right position found*/
        else
        {
            a[i]=a[next];
            i=next;
        }
    }
    a[i]=temp;
}
int findPos(int a[],int n,int x)
{
    /*find the element a[x]'s right position*/
    int i;
    for(i=0;i<n&&a[x]>=a[i];i++);
    return i;
}
void insertNext(int a[],int start,int length)
{
    /*insert a[start] into the sorted part*/
    int i,pos,temp;
    pos=findPos(a,length,start);
    temp=a[start];
    for(i=start;i>pos;i--)
        a[i]=a[i-1];
    a[i]=temp;
}
void checkTwo(int a[],int o[],int n)
{
    int i,pos;
    /*start check if it satisfies the insertion sort*/
    for(i=1;i<n&&a[i]>=a[i-1];i++)
    {
        /*find the first breaking point*/
    }
    pos=i;
    for(;i<n&&a[i]==o[i];i++)
    {
        /*check if the rest of the two arrays' element are same*/
    }

    /*end checking*/
    if(i==n)
    {
        /*satisfied with insertion sort features*/
        printf("Insertion Sort\n");
        insertNext(a,pos,n);
    }
    else
    {
        /*find the heapsort's breaking*/
        for(i=1;i<n&&a[i]<=a[0];i++);  //note me!!!
        printf("Heap Sort\n");
        adjustDown(a,i-1);
    }
}
int main()
{
    int n,i,a[maxNum],o[maxNum];
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        /*original array*/
        scanf("%d",&o[i]);
    }
    for(i=0;i<n;i++)
    {
        /*partly sorted array*/
        scanf("%d",&a[i]);
    }
    checkTwo(a,o,n);

    for(i=0;i<n;i++)
    {
        printf("%d",a[i]);
        if(i<n-1) printf(" ");
        else printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值