实现堆排序算法

堆排序是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。可以利用数组的特点快速定位指定索引的元素。堆分为大根堆和小根堆,是完全二叉树。大根堆的要求是每个节点的值都不大于其父节点的值。在数组的非降序排序中,需要使用的就是大根堆,因为根据大根堆的要求可知,最大的值一定在堆顶。

/**
*    实验题目:
*        实现堆排序算法
*    实验目的:
*        领会堆排序的过程和算法设计
*    实验内容:
*        设计程序,实现堆排序算法。用相关数据进行测试,并
*    输出各趟的排序结果。
*/

#include <stdio.h>

#define MAX_LEN     (100)                       //  最大长度

typedef int key_type;                           //  定义关键字类型为int
typedef char info_type;
typedef struct
{
    key_type key;                               //  关键字项
    info_type data;                             //  其他数据项,类型为info_type
}rec_type;                                      //  查找元素的类型

/*-----------------x和y交换------------------*/
void swap_rec(rec_type &x, rec_type &y)         //  引用类型
{
    rec_type tmp = x;
    x = y;
    y = tmp;
}

/*-----------------创建顺序表------------------*/
void create_list(rec_type recs[], key_type keys[], int n)
{
    int i;

    for(i = 0; i < n; i++)                      // recs[0...n-1]存放排序记录
        recs[i].key = keys[i];
}

/*-----------------输出顺序表------------------*/
void disp_list(rec_type recs[], int n)
{
    int i;

    for(i = 0; i < n; i++)
        printf("%d ", recs[i].key);

    printf("\n");
}

扫描二维码关注公众号,回复: 5324053 查看本文章

/*-----------------以下运算针对堆排序的程序------------------*/
/*-----------------创建顺序表------------------*/
void create_list1(rec_type recs[], key_type keys[], int n)
{
    int i;

    for(i = 1; i <= n; i++)                     // recs[1...n]存放排序记录
    {
        recs[i].key = keys[i - 1];
    }
}

/*-----------------输出顺序表------------------*/
void disp_list1(rec_type recs[], int n)
{
    int i;

    for(i = 1; i <= n; i++)
    {
        printf("%d ", recs[i].key);
    }
    printf("\n");
}

int  cnt = 1;                                           //  全局变量,累计排序趟数

/*------------------以括号表示法输出建立的堆------------------*/
static void disp_heap(rec_type recs[], int i, int n)
{
    if(i <= n)
        printf("%d", recs[i].key);                      //  输出根结点

    if(2 * i <= n || 2 * i + 1 < n)
    {
        printf("(");
        if(2 * i <= n)
            disp_heap(recs, 2 * i, n);                  //  递归调用输出左子树
        printf(",");
        if(2 * i + 1 <= n)
            disp_heap(recs, 2 * i + 1, n);              //  递归调用输出右子树
        printf(")");
    }
}

/*------------------recs[low...high]堆筛选算法------------------*/
static void sift(rec_type recs[], int low, int high)
{
    int i = low;                                        //  recs[j]是recs[i]的左孩子
    int j = 2 * i;
    rec_type temp = recs[i];

    while(j <= high)
    {
        if(j < high && recs[j].key < recs[j + 1].key)   // 若右孩子较大,把j指向右孩子
            j++;                                        //  变为2i+1
        if(temp.key < recs[j].key)
        {
            recs[i] = recs[j];                          //  将recs[j]调整到双亲结点位置上
            i = j;                                      //  修改i和j值,以便继续向下筛选
            j = 2 * i;
        }
        else
            break;                                      //  筛选结束
    }
    recs[i] = temp;                                     //  被筛选结点的值放入最终位置
}

/*------------------对recs[1]到recs[n]元素实现堆排序------------------*/
static void heap_sort(rec_type recs[], int n)
{
    int i, j;

    for(i = n / 2; i >= 1; i--)                         //  循环建立初始堆
        sift(recs, i, n);
    printf("初始堆:");
    disp_heap(recs, 1, n);                              //  输出初始堆9(8(6(2,4),5(0,)),7(1,3))
    printf("\n");

    for(i = n; i >= 2; i--)                             //  进行n-1次循环,完成堆排序
    {
        printf("第%d趟排序:", cnt++);
        printf(" 交换%d与%d,输出%d", recs[i].key, recs[1].key, recs[1].key);
        swap_rec(recs[1], recs[i]);                     //  将第一个元素同当前区间内的recs[1]对换
        printf(" 排序结果:");                           //  输出每一趟的排序结果
        for(j = 1; j <= n; j++)
            printf("%2d", recs[j].key);
        printf("\n");
        sift(recs, 1, i - 1);                           //  筛选recs[1]结点,得到i-1个结点的堆
        printf("筛选调整得到堆:");
        disp_heap(recs, 1, i - 1);
        printf("\n");
    }
}

int main(int argc, char *argv[])
{
    int n = 10;
    rec_type recs[MAX_LEN];
    key_type a[] = {6, 8, 7, 9, 0, 1, 3, 2, 4, 5};

    create_list1(recs, a, n);
    printf("排序前: ");
    disp_list1(recs, n);
    heap_sort(recs, n);
    printf("排序后: ");
    disp_list1(recs, n);

    return 0;
}
测试结果:

排序前: 6 8 7 9 0 1 3 2 4 5
初始堆:9(8(6(2,4),5(0,)),7(1,3))
第1趟排序: 交换0与9,输出9 排序结果: 0 8 7 6 5 1 3 2 4 9
筛选调整得到堆:8(6(4(2,0),5),7(1,3))
第2趟排序: 交换0与8,输出8 排序结果: 0 6 7 4 5 1 3 2 8 9
筛选调整得到堆:7(6(4(2,),5),3(1,0))
第3趟排序: 交换2与7,输出7 排序结果: 2 6 3 4 5 1 0 7 8 9
筛选调整得到堆:6(5(4,2),3(1,0))
第4趟排序: 交换0与6,输出6 排序结果: 0 5 3 4 2 1 6 7 8 9
筛选调整得到堆:5(4(0,2),3(1,))
第5趟排序: 交换1与5,输出5 排序结果: 1 4 3 0 2 5 6 7 8 9
筛选调整得到堆:4(2(0,1),3)
第6趟排序: 交换1与4,输出4 排序结果: 1 2 3 0 4 5 6 7 8 9
筛选调整得到堆:3(2(0,),1)
第7趟排序: 交换0与3,输出3 排序结果: 0 2 1 3 4 5 6 7 8 9
筛选调整得到堆:2(0,1)
第8趟排序: 交换1与2,输出2 排序结果: 1 0 2 3 4 5 6 7 8 9
筛选调整得到堆:1(0,)
第9趟排序: 交换0与1,输出1 排序结果: 0 1 2 3 4 5 6 7 8 9
筛选调整得到堆:0
排序后: 0 1 2 3 4 5 6 7 8 9

猜你喜欢

转载自blog.csdn.net/xiezhi123456/article/details/87286334