中国大学MOOC-陈越、何钦铭-数据结构 Insertion or Heap Sort

来源:互联网 发布:大q吧数据 编辑:程序博客网 时间:2024/05/21 10:49

题目描述:
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 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

思路:
这道题和前一道Insert or Merge思路差不多,在这里就不细说了。

代码如下:

#include<iostream>using namespace std;#define MaxSize 100//定义数组的最大容量void Input(int A[],int N){//输入函数    for (int i = 0; i < N; i++){        cin >> A[i];    }}int Check(int A[], int B[], int N){//遍历两个数组,找出他们元素相等的个数    int total=0;//记录元素相等的个数    for (int i = 0; i < N; i++){        if (A[i] == B[i]){            total++;        }    }    return total;}bool InsertionSort(int A[], int B[], int N){//插入排序算法.A为待排序的数组,B为部分排序的数组    int i, j, total;    int temp;    for (i = 1; i < N; i++){        temp = A[i];        for (j = i; j>0 && A[j - 1] > temp; j--){            A[j] = A[j - 1];        }    A[j] = temp;    total = Check(A, B, N);    if (total == N){//判断是否是插入排序        break;    }    }    if (total == N){//如果是插入排序        i++; temp = A[i];//接下来就是多做一次插入排序        for (j = i; j > 0 && A[j - 1] > temp; j--){            A[j] = A[j - 1];        }        A[j] = temp;        return true;//返回真    }    else return false;//否则返回假}int LeftChild(int i){//返回i的左儿子    return (2 * i + 1);}void PerDown(int A[], int i, int N){//向下过滤    int temp=A[i];    int Child;int Parent = i;    for (; LeftChild(Parent) < N; Parent = Child){        Child = LeftChild(Parent);        if (Child != N - 1 && A[Child] < A[Child + 1]){//找儿子中最大的那一个            Child++;        }        if (temp < A[Child]){ A[Parent] = A[Child]; }        else { break; }    }    A[Parent] = temp;}void DeleteMax(int A[],int i, int j){//删除堆中最大元素,并把它放在适当的位置    int temp = A[i];    A[i] = A[j];    A[j] = temp;}bool HeapSort(int A[], int B[], int N){//堆排序实现    int i;    int total;// total2;    for (i = N / 2; i >=0; i--){//把堆调整为最大堆        PerDown(A, i, N);    }    for (i = N - 1; i>=0; i--){        DeleteMax(A, 0, i);        PerDown(A, 0, i);        total = Check(A, B, N);        if (total == N){ break; }    }    if (total == N){//如果是HeapSort,则再做一次堆排序        i--;        DeleteMax(A, 0, i);        PerDown(A, 0, i);        return true;    }    else { return false; }}void Output(int A[], int N){//输出函数    for (int i = 0; i < N - 1; i++){        cout << A[i] << " ";    }    cout << A[N - 1];}int main(){    int N,i,key;    bool j1,j2;    int A1[MaxSize], A2[MaxSize];//归并排序和插入排序各用一个相同的数组    int B[MaxSize];//储存部分已排序的数    cin >> N;    for (i = 0; i < N; i++){        cin >> key;      A1[i]= A2[i] = key;    }    for (i = 0; i < N; i++){//输入部分已排序的数组        cin >> B[i];    }    j1 = InsertionSort(A1, B, N);    j2 = HeapSort(A2, B, N);    if (j1){        cout << "Insertion Sort" << endl;        Output(A1, N);    }    if(j2){        cout << "Heap Sort" << endl;        Output(A2, N);    }}
0 0
原创粉丝点击