In place heap sorting

Keywords: C Algorithm data structure

Experimental ReportLab

1, Experiment Name: in place heat sorting

2, Purpose of the experiment:

  1. Master the data structure of the largest heap and the characteristics of the structure;
  2. It can realize the basic operations of maximum heap, such as construction, insertion, deletion, initialization, memory recovery, etc
  3. Heap sorting using maximum heap
    3, Experiment content:
  4. Determine the array structures for a complete Max heap
  5. Input a linear list of 20 random numbers, building a complete max-heap to store the numbers by the process of in-place heapification
  6. Implement an in-place sorting, making use of POP operation for the built complete max-heaps .
    (End)
    4, Algorithm implementation:
    The heap sorting algorithm can be divided into two parts.
    In the first step, construct the heap. The heap is usually placed in an array with a complete binary tree layout. The complete binary tree maps the binary tree structure to the array index; Each array index represents a node; The index of a node's parent, left child branch, or right child branch is a simple expression. For zero based arrays, the root node is stored at index 0; If i is the index of the current node, then
    //The heap is stored from node 1 of the array
    If Parent = i,
    LeftChild = 2 * i,
    RightChild = 2 * i
    In the second step, the sorted array is created by repeatedly deleting the largest element from the heap (the root of the heap) and inserting it into the array. The heap is updated after each deletion to maintain heap properties. Once all objects are removed from the heap, the result is a sorted array.
    Heap sorting can be performed in place. The array can be divided into two parts, the sorted array and the heap. The array is stored here as a heap. The heap invariance is preserved after each pop-up, so the only cost of this process is the cost of pop-up.
    Code to implement this step:
    for(i=realSize;i>0;i–){
    h->data[i]=PopMaxHeap(h);
    printf("%d ", h->data[i]);
    }

The Heapsort algorithm involves first converting the array to the maximum heap. Then, the first value of the list is repeatedly exchanged with the last value, and the range of values considered in the heap operation is reduced by one,
. Repeat this process until the range length of the values considered for the heap operation is one.

These steps are:

  1. Call the buildMaxHeap() function on the array. It builds a heap from the array in the O (n) operation.
  2. Swap the first element of the array with the last element. Reduce the consideration of heap operation by one. (the program directly makes the last element of the UN ejected heap equal to the ejected element. The ejecting process reduces the consideration scope of heap operation by one.)
  3. Call the FixDown() function in the array to filter the new first element to the corresponding index in the heap.
  4. Unless the array is considered to be an element, go to step (2).
    The buildMaxHeap() operation is run once and the performance is O (n). The FixDown() function is O (log n) and is called n times. Therefore, the performance of the algorithm is O (n + n log n) = O (n log n).

4, Program list (more detailed comments):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MaxSize 100
typedef struct Hnode* MaxHeap;
struct Hnode{
    int* data;
    int size;
    int Capacity;
};
int getRandomNum(){
    int RandomNum;
    RandomNum=rand();
    return  RandomNum;
}
/*Filter down and find the proper position of the node on the premise that all subtrees are the MaxHeap. 
The tree with this node as the root node is a smallest heap*/
void FixDown(MaxHeap h,int index){
    int parent;
    int child;
    int temp=h->data[index];
    for(parent=index;parent*2<=h->size;parent=child){
        child=parent*2;
        if(child+1<=h->size&&h->data[child+1]>h->data[child])
            child++;
        if(h->data[child]>temp){
            h->data[parent]=h->data[child];
        }
        else break;
    }
    h->data[parent]=temp;
}
/*The idea of recursion is adopted to establish the minimum heap from bottom to top, which ensures 
that the subtrees currently established are all the smallest heaps.*/
void BuildMaxHeap(MaxHeap h){
    int i;
    for(i=h->size/2;i>=1;i--){
        FixDown(h,i);
    }
}
//Determine whether the MinHeap is empty
int Empty(MaxHeap h){
    return h->size==0;
}
//Return and delete the max value of the max heap (that is, the head node), and keep the tree as the max heap
/*Move the last node to the head node, and size--,
Because the subtrees are all the MaxHeap at this time, filtering directly 
down can ensure that the tree is still the MaxHeap*/
int PopMaxHeap(MaxHeap h){
    if(Empty(h)){
        printf("The MaxHeap is empty, and the popped -11111 is an invalid value\n");
        return -11111;
    }
    int result=h->data[1];
    h->data[1]=h->data[(h->size)--];
    //Filter down
    FixDown(h,1);
    return result;
}
/*Insert node, keep MaxHeap*/
void PushMaxHeap(MaxHeap h,int val){
    if(h->size==h->Capacity){
        printf("The minimum heap is full and element insertion is invalid\n");
        return;
    }
    h->data[++(h->size)]=val;
    //Upward filtering
    int child=h->size;
    int parent;
    for(;child>1;child=parent){
        parent=child/2;
        if(h->data[parent]<val){
            h->data[child]=h->data[parent];
        }
        else
        {
            break;
        }
        
    }
    h->data[child]=val;
}
/*Request space for the MaxHeap*/
MaxHeap CreateMaxHeap(int MaxHeapSize){
    MaxHeap h=(MaxHeap)malloc(sizeof(MaxHeap));
    h->data=(int*)malloc((MaxHeapSize+1)*sizeof(int));
    h->size=0;
    h->Capacity=MaxHeapSize;
    return h;
}
/*Free up space for MinHeap*/
void CleanMaxHeap(MaxHeap h){
    free(h->data);
    free(h);
}
int main()
{
    MaxHeap h=CreateMaxHeap(MaxSize);

    //Construct the MaxHeap with a random array of 20 numbers, and pop the MaxHeap number in turn
    printf("Construct the MaxHeap with a random array of 20 numbers\n");
    srand((unsigned)time(NULL));
    int i=1;
    printf("20 random numbers:\n");
    for(;i<=20;i++){
        h->data[i]=getRandomNum();
        printf("%d ",h->data[i]);
        h->size++;
    }
     printf("\n");
     int realSize=h->size;//The true size of the array
    BuildMaxHeap(h);
    //After the MaxHeap is established, pop the element
    /*Create a sorted array by repeatedly deleting the largest 
    element from the heap (the root of the heap) and then inserting it into the array*/
    printf("After the MaxHeap is established, pop the element:\n");
    for(i=realSize;i>0;i--){
         h->data[i]=PopMaxHeap(h);
        printf("%d ", h->data[i]);
    }
    printf("\nAfter sorting the heap, output the result of the ordered array from small to large:\n");
    for(i=1;i<=realSize;i++){
        printf("%d ",h->data[i]);
    }
    CleanMaxHeap(h);
    return 0;
}

5, Test results:

Input / output format description:

  • Input format:
    nothing

  • Output format:
    First output the contents of 20 random arrays;
    Then output the maximum heap constructed by the array and pop up the contents in turn to realize heap sorting;

  • Test example 1:

    Input:

    Output:

      Construct the MaxHeap with a random array of 20 numbers
      20 random numbers:
      2503 10087 8316 4624 28366 11343 11 2506 8354 14096 15705 32370 27966 16578 1005 26874 14107 31248 31125 28811
      After the MaxHeap is established, pop the element:
      32370 31248 31125 28811 28366 27966 26874 16578 15705 14107 14096 11343 10087 8354 8316 4624 2506 2503 1005 11
      After sorting the heap, output the result of the ordered array from small to large:
      11 1005 2503 2506 4624 8316 8354 10087 11343 14096 14107 15705 16578 26874 27966 28366 28811 31125 31248 32370
    
  • Test example 2:

    Input:

    Output:

      Construct the MaxHeap with a random array of 20 numbers
      20 random numbers:
      4511 1221 17501 25194 14838 1450 13625 4472 4902 1354 23046 26691 11887 1720 17697 7431 24869 25033 10137 13027
      After the MaxHeap is established, pop the element:
      26691 25194 25033 24869 23046 17697 17501 14838 13625 13027 11887 10137 7431 4902 4511 4472 1720 1450 1354 1221
      After sorting the heap, output the result of the ordered array from small to large:
      1221 1354 1450 1720 4472 4511 4902 7431 10137 11887 13027 13625 14838 17501 17697 23046 24869 25033 25194 26691
    
  • Test example 3:

    Input:

    Output:

      Construct the MaxHeap with a random array of 20 numbers
      20 random numbers:
      4577 19581 14336 14940 24521 14449 10338 11463 7453 9465 20088 27571 28680 4967 2788 26513 4173 321 13984 24768
      After the MaxHeap is established, pop the element:
      28680 27571 26513 24768 24521 20088 19581 14940 14449 14336 13984 11463 10338 9465 7453 4967 4577 4173 2788 321
      After sorting the heap, output the result of the ordered array from small to large:
      321 2788 4173 4577 4967 7453 9465 10338 11463 13984 14336 14449 14940 19581 20088 24521 24768 26513 27571 28680
    
  • Test example 4:

    Input:

    Output:

      Construct the MaxHeap with a random array of 20 numbers
      20 random numbers:
      4636 16445 8211 22096 13575 19594 826 24308 9750 29872 17426 28364 1196 7889 9031 10919 28144 4295 14168 22228
      After the MaxHeap is established, pop the element:
      29872 28364 28144 24308 22228 22096 19594 17426 16445 14168 13575 10919 9750 9031 8211 7889 4636 4295 1196 826
      After sorting the heap, output the result of the ordered array from small to large:
      826 1196 4295 4636 7889 8211 9031 9750 10919 13575 14168 16445 17426 19594 22096 22228 24308 28144 28364 29872
    

6, Algorithm analysis:
The insertion and deletion operations of the maximum heap are established based on the tree structure. The time complexity of each operation is O (log n) time,
If the algorithm requires n insertions and each deletion, the total time complexity of insertion and deletion is O ((n+m)log n);
The maximum heap with n nodes is constructed. Similarly, its time complexity is O (nlog n);
The space complexity of the algorithm is the memory space of the maximum heap data structure (tree structure in the form of array)

Heap sorting is more stable in the sorting method, and the time complexity is stable at O (nlog n), but its disadvantage is that it needs to be stored in logarithm. If you want to get an ordered sequence after constructing the maximum heap,
It needs to open up double array memory space, which has high space complexity and is not suitable for sorting a large amount of data.

Posted by PJSheltrum on Fri, 15 Oct 2021 23:10:31 -0700