堆排序

Posted 一个万能的盒子,它的名字叫innobase

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了堆排序相关的知识,希望对你有一定的参考价值。

  所谓的堆排序,就是把数组虚拟成二叉堆。其定义必须满足如下2个条件中的一个条件:
    
    假设数组a[n]
        (1) 二叉堆中的父节点a[r] 必须大于等于子节点
        (2)二叉堆的父节点必须小于等于子节点

        Parent(i):
                return a[(i]
        Left(i):
                return a[(i+1)<<1-1]
        Right(i):
                return a[(i+1)<<1]
   
    二叉堆的维护(移动对应节点的值):
        a的数组大小为 asize
        二叉堆当前的有效元素的大小为psize
        adjustheap(a,i):
            left = (i+1)<<1-1;
            right = (i+1)<<1;
            big = i
            if left< psize && a[left] >= a[i]
                big =left 
            if right < psize && a[right] >= a[i]
                big = right
            if big != i
                swap(a[big],a[i]
                adjustheap(a,big)
     建堆:
          build(a):
            psize = asize
            for i= psize>>2-1 downto 1
                    adjustheap(a,i)
      排序:
         sort(a):
           bulid(a)
           psize = size -1
           for i = psize and i>0 and i--:
                 swap(a[i],a[0])
                 psize--;
                 adjustheap(a[i],0)
/*************************************************************************
        > File Name: hsort.h
        > Author: zhoulin
        > Mail: [email protected]
        > Created Time: Tue 29 Mar 2016 11:30:45 PM CST
 ************************************************************************/

#ifndef _HEAPSORT_H
# define _HEAPSORT_H
typedef struct _heap
{
    int *arr;//target array
    int  len;//arr len
    int  index;
}heap;
//remove a data from heap
int RemoveData(heap *hp,int v);
//insert a data into heap
int InsertData(heap *hp,int v);
//adjust a heap
void AdjustHeapSmall(heap *hp,int i);
void AdjustHeapBig(heap *hp,int i);
//sort heap
void SortHeapBig(heap *hp);
void SortHeapSmall(heap *hp);
#endif
/*************************************************************************
        > File Name: heapsort.c
        > Author: zhoulin
        > Mail: [email protected]
        > Created Time: Wed 30 Mar 2016 03:39:20 AM CST
 ************************************************************************/
#include "heapsort.h"
#include <stdio.h>
static void Swap(int *a,int *b)
{
    *a = *a^*b;
    *b = *a^*b;
    *a = *a^*b;
}
void AdjustHeapBig(heap *hp,int i)
{
    unsigned int left = (i+1)<<1-1; // array is start with 0
    unsigned int right = (i+1)<<1;
    unsigned int big = i;
    int *arr = hp->arr;
    unsigned int max = hp->index;
    if(left <= max && arr[left] <= arr[i])
    {
        big = left;
    }
    if(right <= max && arr[right] <= arr[big])
    {
        big = right;
    }
    if(big != i)
    {
        Swap(&arr[big],&arr[i]);
        AdjustHeapBig(hp,big);
    }
}
void AdjustHeapSmall(heap *hp,int i)
{
    unsigned int left = (i+1)<<1-1; // array is start with 0
    unsigned int right = (i+1)<<1;
    unsigned int small = i;
    int *arr = hp->arr;
    unsigned int max = hp->index;
    if(left <= max && arr[left] >= arr[i])
    {
        small = left;
    }
    if(right <= max && arr[right] >= arr[small])
    {
        small = right;
    }
    if(small != i)
    {
        Swap(&arr[small],&arr[i]);
        AdjustHeapSmall(hp,small);
    }
}
void SortHeapBig(heap *hp)
{
    int *arr = hp->arr;
    int size = hp->len;
    int i;
    for(i = size>>1-1;i >= 0; i--)
    {
        AdjustHeapBig(hp,i);
    }
    while(hp->index > 0)
    {
        Swap(&arr[0],&arr[hp->index]);
        hp->index--;
        AdjustHeapBig(hp,0);
    }
}
void SortHeapSmall(heap *hp)
{
    int *arr = hp->arr;
    int size = hp->len;
    int i;
    for(i = size>>1-1;i >= 0; i--)
    {
        AdjustHeapSmall(hp,i);
    }
    while(hp->index > 0)
    {
        Swap(&arr[0],&arr[hp->index]);
        hp->index--;
        AdjustHeapSmall(hp,0);
    }
}
int main(void)
{
    int i;
    int arr[11] = {19,2,45,80,21,56,1,100,3,99,19};
    fprintf(stdout,"****************src array******************\n");
    for(i = 0; i< 11;i++)
    {
        fprintf(stdout,"arr[%d] =%d\n",i,arr[i]);
    }
    heap p;
    p.len = 11;
    p.arr = arr;
    p.index = p.len -1;
    SortHeapBig(&p);
    fprintf(stdout,"****************big heap sort******************\n");
    for(i = 0; i< p.len;i++)
    {
        fprintf(stdout,"arr[%d] =%d\n",i,arr[i]);
    }
    int arr1[11] = {19,2,45,80,21,56,1,100,3,99,19};
    p.len = 11;
    p.arr = arr1; 
    p.index = p.len -1;
    SortHeapSmall(&p);
    fprintf(stdout,"****************small heap sort******************\n");
    for(i = 0; i< p.len;i++)
    {
        fprintf(stdout,"arr[%d] =%d\n",i,arr1[i]);
    }
    return 0;
}

运行结果:

[email protected]:~/code/c_src/heapsort:./heapsrot 
****************src array******************
arr[0] =19
arr[1] =2
arr[2] =45
arr[3] =80
arr[4] =21
arr[5] =56
arr[6] =1
arr[7] =100
arr[8] =3
arr[9] =99
arr[10] =19
****************big heap sort******************
arr[0] =100
arr[1] =99
arr[2] =80
arr[3] =56
arr[4] =45
arr[5] =21
arr[6] =19
arr[7] =19
arr[8] =3
arr[9] =2
arr[10] =1
****************small heap sort******************
arr[0] =1
arr[1] =2
arr[2] =3
arr[3] =19
arr[4] =19
arr[5] =21
arr[6] =45
arr[7] =56
arr[8] =80
arr[9] =99
arr[10] =100

 

以上是关于堆排序的主要内容,如果未能解决你的问题,请参考以下文章

选择排序(简单选择排序堆排序的算法思想及代码实现)

排序--08---堆排序

python代码实现堆排序

算法-java代码实现堆排序

一文带你了解堆排序

堆排序