The order of each heap adjustment in heap sorting

Heap sort (heap sort) is an improvement to simple selection sorting. The focus of the improvement is: how to reduce the number of record comparisons. Simple selection sorting selects only the smallest record in a sorting pass, and does not save the comparison results of a pass, so the comparison times of records are more. While selecting the smallest record, the heap sort also finds smaller records, reducing the number of comparisons selected, thereby improving the efficiency of the entire sort. The basic idea of ​​the heap sorting algorithm is as follows:
first construct the sequence to be sorted into a large root heap, that is, select the largest of all records in the heap, remove it from the heap, and adjust the remaining records into a heap, so I found the next smallest record, and so on, until there was only one record in the heap.
Now I hope to calculate the order of the heap after each heap adjustment (including the construction of the initial heap) during the sorting process from small to large heap.
Insert picture description here
[Input form]
An integer N in the first line, N integers in the second line, unordered. 0 <N <= 100
[Output form]
N lines of output are output. The i-th row corresponds to the order of N-i + 1 integers after the i-th heap adjustment.
[Sample input]
5
2 3 1 5 4
[Sample output]
5 4 1 3 2
4 3 1 2
3 2 1
2 1
1

Analysis:
heap sort properties using construct a complete binary tree root large heap (heap rootlets) the maximum (minimum) into the root node of the root node and the last node with the value exchange
is essentially constructed using repeated large root stack (small Root heap) method, find the maximum (minimum) value and then sort it by swapping with the last node

Function to build heap

void adjust(int arr[],int n,int i)
{
    int left=i*2+1;//下标为i的左孩子
    int right=i*2+2;//下标为i的右孩子
    int maxi = i;
    if(left<n&&arr[left]>arr[maxi])
        maxi=left;
    if(right<n&&arr[right]>arr[maxi])
        maxi=right;
    if(maxi!=i)//当下标为i 的结点不是它和它孩子节点中最大数时
    {
        swap(arr[maxi],arr[i]);
        adjust(arr,n,maxi);
    }
}

Function to sort the output heap

void heapSort(int arr[],int n)
{
    for(int i=n/2-1;i>=0;i--)//从第一个非叶子结点开始
    {
        adjust(arr,n,i);//初始建堆
    }
    for(int i =0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
    for(int i=n-1,j=n-1;i>=1;i--,j--)//经过初始建堆后 还需n-1次建堆
    //每次最大的在树的根节点上
    {
        swap(arr[0],arr[i]);//把根节点和最后一个结点交换
        adjust(arr,i,0);//继续进行排堆 从下标为0的结点开始 对剩下的i个进行堆排序
        for(int i=0;i<j;i++)
        {
            cout<<arr[i]<<" ";//输出堆排序后每次堆调整后的次序
        }
        cout<<endl;
    }
}

Main function

int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    heapSort(arr,n);
}

Below is a little white. If there is an inappropriate place, please correct me.

Published 31 original articles · won praise 8 · views 2155

Guess you like

Origin blog.csdn.net/weixin_44034024/article/details/105242476