MOOC Zhejiang University Data Structure-09-Sort 3 Insertion or Heap Sort (25 points)

This question is exactly the same as the previous question. MOOC Zhejiang University Data Structure-09-Sort 2 Insert or Merge (25 points) , the difference is that merge sort is replaced with heap sort, also refer to 09-Sort 3 Insertion or Heap Sort (25 points) In the process of writing heap sort, the errors are as follows:

  1. temp = A[f];//I put this sentence inside the loop and caused an error. This is caused by unclear thinking. We assign temp outside the loop and assign its value to the root node element of the largest heap, and then This element is continuously compared downwards to find a position for him. If a value is assigned within the loop, the value of this temp will change with each loop, so it is wrong.
  2. if(temp>=A[child]) break;//wrong used A[temp], this is an error caused by not knowing whether the value stored by the temp variable is an array subscript or an element value
  3. if(temp>=A[child]) break;//here> or write it as>=. Reason: to maintain the stability of the sorting algorithm (the stability of the sorting algorithm and its significance assume that there are multiple records with the same key in the sequence of records to be sorted. If sorted, the relative order of these records remains unchanged. That is, in the original sequence, ri=rj, and ri is before rj, and in the sorted sequence, ri is still before rj, this sorting algorithm is said to be stable; otherwise it is called unstable.)
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 105
int A[MaxSize],CopyA[MaxSize],B[MaxSize];
int IsInsertFlag=0;
int IsIdentical(int A[],int n){
    
    
    int i;
    for(i=0;i<n;++i){
    
    
        if(A[i]!=B[i]) return 0; 
    }
    return 1;
}
void Insert_sort(int A[],int n){
    
    
    int i,j,flag=0,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;
        if(IsIdentical(A,n)){
    
    
            printf("Insertion Sort\n");
            flag = 1;
            IsInsertFlag = 1;
            continue;
        }
        if(flag) break;
    }
}
void swap(int *a,int *b){
    
    
    int temp = *a;
    *a = *b;
    *b = temp;
}
void HeapAdjust(int A[],int f,int e){
    
    //f and e为存储最大堆的数组第一个元素和最后一个元素的下标
    int parent,child,temp;
    temp = A[f];//我把这句话放到循环里面导致错误
    for(parent=f;(2*parent+1)<=e;parent=child){
    
    
        child = 2*parent+1;
        if(child<e&&A[child+1]>A[child]) child++;
        if(temp>=A[child]) break;//这里>还是写成>=吧 wrong used A[temp]
        else{
    
    
            A[parent] = A[child];
        }
    }
    A[parent] = temp;
}
void Heap_sort(int A[],int n){
    
    
    int i,j,flag=0;
    for(i=n/2-1;i>=0;--i){
    
    
        HeapAdjust(A,i,n-1);
    }
    for(j=n-1;j>0;--j){
    
    
        swap(&A[0],&A[j]);
        HeapAdjust(A,0,j-1);
        if(IsIdentical(A,n)){
    
    
            flag = 1;
            printf("Heap Sort\n");
            continue;
        }
        if(flag) break;
    }
}
int main(){
    
    
    int n,i;
    scanf("%d",&n);
    for(i=0;i<n;++i){
    
    
        scanf("%d",&A[i]);
        CopyA[i] = A[i];
    }
    for(i=0;i<n;++i){
    
    
        scanf("%d",&B[i]);
    }
    Insert_sort(A,n);
    if(IsInsertFlag){
    
    
        printf("%d",A[0]);
        for(i=1;i<n;++i){
    
    
            printf(" %d",A[i]);
        }
    }
    else{
    
    
        Heap_sort(CopyA,n);
        printf("%d",CopyA[0]);
        for(i=1;i<n;++i){
    
    
            printf(" %d",CopyA[i]);
        }        
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43919570/article/details/105850459