Heap Sort 堆排序C语言实现

#Heap Sort-堆排序

此贴由戴维营教育学员翻译,鄙人整理,特意为戴维营教育零基础学员课外学习之用.

##堆排序 :
堆排序就是使用堆来对数组进行排序。而堆数据结构,可以被看作是一个完整的,平衡二叉树的数组对象。堆分大根堆和小根堆,小根堆(大根推)都有个属性:除了根以外的每一个节点,其值至少(至多)是该节点的父节点的值。由这个性质可知大根堆的堆顶的值肯定是所有值中最大的,小根堆的堆顶的值是所有值中最小的,因此,堆中最小的(最大的)元素被存储在根节点,而且一个节点的子树包含一些比自己较大的(较小的)节点。

##算法:
它通过插入由用户输入的元素到对应位置来构建一个堆:
1,增加堆的大小用存储插入的值.
2,将输入值插在最后的地方.
3,将插入值与其父节点的值比较,直到满足堆属性,然后将其放置在其正确位置.

1, heap[1]是最小的元素,因此,我们删除heap[1],堆的大小减小。
2,现在heap[1]必须用新值来填补,我们把最后一个元素放这地方,看看它是否适合;如果不适合,采取两子节点中的最小元素替代父节点。
3,再看看最后一个元素在那个地方是否适合。

##特性:
最好情况下执行 - 当输入数组早已排序好, 时间复杂度: O(nlogn)
最坏情况下执行 - 当输入数组是倒序, 时间复杂度: O(nlogn);
平均情况下执行 - 时间复杂度: O(nlogn);
它不需要一个额外的空间来排序,因此空间复杂度是: O(1)
它是不稳定的排序算法;

##堆排序C语言实现

#include <stdio.h>
#include <limits.h>
/*Declaring heap globally so that we do not need to pass it as an argument every time*/

/* Heap used here is Min Heap */
int heap[1000000],heapSize;

/*Initialize Heap*/
void Init()
{
        heapSize = 0;
        heap[0] = -INT_MAX;
}

/*Insert an element into the heap */
void Insert(int element)
{
        heapSize++;
        heap[heapSize] = element; /*Insert in the last place*/
        /*Adjust its position*/
        int now = heapSize;
        while(heap[now/2] > element)
        {
                heap[now] = heap[now/2];
                now /= 2;
        }
        heap[now] = element;
}

int DeleteMin()
{
        /* heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.Now heap[1] has to be filled. We put the last element in its place and see if it fits.If it does not fit, take minimum element among both its children and replaces parent with it.Again See if the last element fits in that place.*/
        int minElement,lastElement,child,now;
        minElement = heap[1];
        lastElement = heap[heapSize--];
        /* now refers to the index at which we are now */
        for(now = 1; now*2 <= heapSize ;now = child)
        {
                /* child is the index of the element which is minimum among both the children */
                /* Indexes of children are i*2 and i*2 + 1*/
                child = now*2;
                /*child!=heapSize beacuse heap[heapSize+1] does not exist, which means it has only one child */
                if(child != heapSize && heap[child+1] < heap[child] )
                {
                        child++;
                }
                /* To check if the last element fits ot not it suffices to check if the last element is less than the minimum element among both the children*/
                if(lastElement > heap[child])
                {
                        heap[now] = heap[child];
                }
                else /* It fits there */
                {
                        break;
                }
        }
        heap[now] = lastElement;
        return minElement;
}

int main(void)
{
        int number_of_elements;
        scanf("%d",&number_of_elements);
        int iter, element;
        Init();
        for(iter = 0;iter < number_of_elements;iter++)
        {
                scanf("%d",&element);
                Insert(element);
        }
        for(iter = 0;iter < number_of_elements;iter++)
        {
                printf("%d ",DeleteMin());
        }
        printf("\n");
        return 0;
}

标签:none

添加新评论