Learning algorithm: HEAPSORT (big top stack)

1. What is the heap?

Heap is a nonlinear structure, can be seen as a heap array, can also be considered a complete binary tree, popular in terms of the heap is actually a one-dimensional array using the complete binary tree structure to maintain, according to the characteristics of the stack can heap divided into large and small roof top heap heap.

Big Top stack: the value of each root is equal to or greater than the left and right child node value

Small top stack: the value of each root which is less than or equal to the value of the left and right child node

If the root node is labeled as x, the left child of the subscript 2x + 1, the right child subscript 2x + 2

 

2 difference, heap and ordinary tree

Memory used: ordinary tree occupies minimal memory space to be more than the data they store, and heap using only arrays, and does not use pointers. (Although you can use an ordinary tree to simulate heap, but wasted space is relatively large, it is not recommended to do so)

Search: In the binary tree search will soon, but will be very slow search in the heap, the heap is not the first priority in the search, because the purpose is to heap maximum (or minimum) of the junction on the front, so rapid insertion and deletion of related operations

 

3, the top of the heap achieve big ideas

Step: First unordered sequence of n elements to construct a large stack top

Step two: the root with the last element exchange position, (the largest element "sink" to the end of the array)

The third step: after the switching condition may no longer meet the large pile top, it is necessary to the remaining n-1 element constructed to be larger than the new stack top

The fourth step: repeating the above operations, the sort is complete until the entire array

 

//判断是否为大顶堆
void headp_adjust(int *a, int start, int end)
{
    int root = start;
    int child;    //保存最大的孩子的下标
    child = 2*root + 1;    //假设最大孩子为左孩子

    while(child <= end)
    {
        //将结点的左右子树的最大值求出
        if(child + 1 <= end && a[child+1] >= a[child])
            child++;

        //如果根结点的值比孩子的最大值要小,则交换位置
        if(a[root] < a[child])
        {
            a[root] = a[root]^a[child];
            a[child] = a[root]^a[child];
            a[root] = a[root]^a[child];
            
            //将根调整为他的最大的孩子
            root = child;
            child = 2*root + 1;
        }
        else
            return;
    }
}
//堆排序
void heap_sort(int *a, int len)
{
    int i;    //结点在数组中的下标,最后一个非叶子结点的下标为len/2 - 1

    //从最后一个非叶子结点开始,将整个数组变成一个大顶堆
    for(i = len/2-1; i >= 0; i--)
    {
        headp_adjust(a, i, len-1);
    }
    //循环
    for(i = len-1; i > 0; i--)
    {
        //将根结点和最后一个没有交换过的结点交换位置
        a[0] = a[0]^a[i];
        a[i] = a[0]^a[i];
        a[0] = a[0]^a[i];
        
        //将整个数组调整成大顶堆
        headp_adjust(a, 0, i-1);
    }
}

int main(void)
{
    int a[6] = {9, 6, 2, 4, 6, 4};
    heap_sort(a, 6);

    int i;
    for(i = 0; i < 6; i++)
        printf("%d ", a[i]);
    printf("\n");

    return 0;
}

 

 

 

 

 

 

Published 50 original articles · won praise 5 · Views 1530

Guess you like

Origin blog.csdn.net/qq_42483691/article/details/104591024