• 堆排序的实现代码


    已经在VC6下运行过。

    //堆排序
    //默认非叶子节点i以下的节点都已经排好次序,已经成为排好次序的最大/小堆
    void HeapAdjust(int* source, int s, int t)
    {
        int temp,j;
        temp=source[s];
       for (j=2*s+1;j<=t;j=2*j+1)//沿着关键字较大的孩子节点向下筛选,要特别注意这里的转换j=2*s+1
        {                         //因为这个是针对数组的下标来做的,从0开始而不是从1开始
            if (j<t && source[j]<source[j+1])
              ++j;
            if(temp>=source[j])
                break;
            source[s]=source[j];
            s=j;
        }
        source[s]=temp;//插入
    }

    void HeapSort(int* source, int length)//length为数组的长度,而不是最大下标
    {
        int i;
        for (i=(length-2)/2;i>=0;i--)//堆化数组
        {
            HeapAdjust(source,i,length-1);
        }

        for (i=length-1;i>0;i--)
        {
            Swap(source,0,i);
            HeapAdjust(source,0,i-1);
        }
    }

  • 相关阅读:
    keepalived安装
    Nginx学习笔记
    使用xhprof分析php性能
    使用 .bash_profile与.bashrc修改字符集
    Mysql分区简述
    c语言多线程队列读写
    setsockopt 设置 SO_LINGER 选项
    nginx配置rewrite
    使用PHP+ajax打造聊天室应用
    UDP/TCP通信小记
  • 原文地址:https://www.cnblogs.com/jiayouwyhit/p/3214230.html
Copyright © 2020-2023  润新知