仿STL中的堆算法的一个实现

RT。

堆的性质之类的不再这里阐述,写这个算法只为了更好的理解STL中的堆算法,如果看不懂STL中的算法也可以来参考这里给出的算法,因为是纯C的看起来会省去很多语言方面的细节。

同时里面还有一个STL中对应算法的测试以比较两者的效果。

/********************************************************************    created:    2007/3/18    filename:     main.cpp    author:        Lichuang    purpose:    测试模拟堆算法*********************************************************************/#include <algorithm>#include <iostream>#include <time.h>using namespace std;// push_heap为向堆中添加一个新的元素, 调用这个算法的前提是[First, Last)之间的元素满足堆的条件// 新加入的元素为Lastvoid  push_heap(int* pFirst, int* pLast);// pop_heap为从堆中删除一个元素, 调用这个算法的前提是[First, Last)之间的元素满足堆的条件// 被删除的元素被放置到Last - 1位置,由于这里是max-heap,所以被删除的元素是这个序列中最大的元素void    pop_heap(int* pFirst, int* pLast);// make_heap将序列[First, Last)中的元素按照堆的性质进行重组void    make_heap(int* pFirst, int* pLast);// 对堆进行排序, 调用这个函数可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性质void    sort_heap(int* pFirst, int* pLast);// 判断一个序列[First, Last)是否满足堆的条件,是就返回1,否则返回0char    is_heap(int* pFirst, int* pLast);void    test_heap_algo(int *pArray, int nLength);void    test_heap_algo_in_stl(int *pArray, int nLength);void    display_array(int *pArray, int nLength);int main(){    srand(time(NULL));    int Array[10], Array2[10];    for(int i = 0; i < 10; ++i)        Array[i] = Array2[i] = rand();    test_heap_algo(Array, sizeof(Array) / sizeof(int));    test_heap_algo_in_stl(Array2, sizeof(Array2) / sizeof(int));    return 0;}// 静态函数, 用于根据堆的性质调整堆static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue);// push_heap为向堆中添加一个新的元素, 调用这个算法的前提是[First, Last)之间的元素满足堆的条件// 新加入的元素为Lastvoid push_heap(int* pFirst, int* pLast){    int nTopIndex, nHoleIndex, nParentIndex;    int nValue;    nTopIndex = 0;    nHoleIndex = (int)(pLast - pFirst - 1);    nParentIndex = (nHoleIndex - 1) / 2;    nValue = *(pLast - 1);    // 如果需要插入的节点值比父节点大, 上溯继续查找    while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)    {        pFirst[nHoleIndex] = pFirst[nParentIndex];        nHoleIndex = nParentIndex;        nParentIndex = (nHoleIndex - 1) / 2;    }    pFirst[nHoleIndex] = nValue;}// pop_heap为从堆中删除一个元素, 调用这个算法的前提是[First, Last)之间的元素满足堆的条件// 被删除的元素被放置到Last - 1位置,由于这里是max-heap,所以被删除的元素是这个序列中最大的元素void pop_heap(int* pFirst, int* pLast){    int nValue;    nValue = *(pLast - 1);    *(pLast - 1) = *pFirst;    adjust_heap(pFirst, 0, (int)(pLast - pFirst - 1), nValue);}// make_heap将序列[First, Last)中的元素按照堆的性质进行重组void make_heap(int* pFirst, int* pLast){    int nLen, nParentIndex;    nLen = (int)(pLast - pFirst);    nParentIndex = (nLen - 1) / 2;    while (true)     {        // 对父节点进行调整, 把父节点的值调整到合适的位置        adjust_heap(pFirst, nParentIndex, nLen, pFirst[nParentIndex]);        if (0 == nParentIndex)             return;        nParentIndex--;    }}// 对堆进行排序, 调用这个函数可以成功排序的前提是[pFirst, pLast)中的元素符合堆的性质void sort_heap(int* pFirst, int* pLast){    // 调用pop_heap函数, 不断的把当前序列中最大的元素放在序列的最后    while(pLast - pFirst > 1)        pop_heap(pFirst, pLast--);}// 判断一个序列[First, Last)是否满足堆的条件,是就返回1,否则返回0char is_heap(int* pFirst, int* pLast){    int nLen, nParentIndex, nChildIndex;    nLen = (int)(pLast - pFirst);    nParentIndex = 0;    for (nChildIndex = 1; nChildIndex < nLen; ++nChildIndex)    {        if (pFirst[nParentIndex] < pFirst[nChildIndex])            return 0;        // 当nChildIndex是偶数时, 那么父节点已经和它的两个子节点进行过比较了        // 将父节点递增1        if ((nChildIndex & 1) == 0)            ++nParentIndex;    }    return 1;}// 一个静态函数仅供adjust_heap调用以证实JJHOU的结论static void push_heap(int *pFirst, int nHoleIndex, int nTopIndex, int nValue){    int nParentIndex;    nParentIndex = (nHoleIndex - 1) / 2;    while (nHoleIndex > nTopIndex && pFirst[nParentIndex] < nValue)    {        pFirst[nHoleIndex] = pFirst[nParentIndex];        nHoleIndex = nParentIndex;        nParentIndex = (nHoleIndex - 1) / 2;    }    pFirst[nHoleIndex] = nValue;}// 对堆进行调整, 其中nHoleIndex是目前堆中有空洞的节点索引, nLen是待调整的序列长度// nValue是需要安插进入堆中的值static void adjust_heap(int *pFirst, int nHoleIndex, int nLen, int nValue){    int nTopIndex, nSecondChildIndex;    nTopIndex = nHoleIndex;    nSecondChildIndex = 2 * nTopIndex + 2;    while (nSecondChildIndex < nLen)    {        if (pFirst[nSecondChildIndex] < pFirst[nSecondChildIndex - 1])            --nSecondChildIndex;        pFirst[nHoleIndex] = pFirst[nSecondChildIndex];        nHoleIndex = nSecondChildIndex;        nSecondChildIndex = 2 * nHoleIndex + 2;    }    if (nSecondChildIndex == nLen)    {        pFirst[nHoleIndex] = pFirst[nSecondChildIndex - 1];        nHoleIndex = nSecondChildIndex - 1;    }    // 以下两个操作在这个函数中的作用相同, 证实了<<STL源码剖析>>中P178中JJHOU所言    //pFirst[nHoleIndex] = nValue;    push_heap(pFirst, nHoleIndex, nTopIndex, nValue);}void    test_heap_algo(int *pArray, int nLength){    std::cout << "\ntest_heap_algo()\n";    make_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    push_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    pop_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    if (is_heap(pArray, pArray + nLength - 1))    {        std::cout << "is heap!\n";    }    else    {        std::cout << "is not heap!\n";    }    make_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    if (is_heap(pArray, pArray + nLength))    {        std::cout << "is heap!\n";    }    else    {        std::cout << "is not heap!\n";    }    sort_heap(pArray, pArray + nLength);    display_array(pArray, nLength);}void    test_heap_algo_in_stl(int *pArray, int nLength){    std::cout << "\ntest_heap_algo_in_stl()\n";    std::make_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    std::push_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    std::pop_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    // 注意is_heap不是STL中支持的算法, 貌似只有SGI的实现才有这个函数!    if (is_heap(pArray, pArray + nLength - 1))    {        std::cout << "is heap!\n";    }    else    {        std::cout << "is not heap!\n";    }    std::make_heap(pArray, pArray + nLength);    display_array(pArray, nLength);    if (is_heap(pArray, pArray + nLength))    {        std::cout << "is heap!\n";    }    else    {        std::cout << "is not heap!\n";    }    std::sort_heap(pArray, pArray + nLength);    display_array(pArray, nLength);}void    display_array(int *pArray, int nLength){    for (int i = 0; i < nLength; ++i)        std::cout << pArray[i] << " ";    std::cout << std::endl;}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索算法
, int
, 元素调用
, 堆
, 堆排序
, heap
, 元素
, 序列
, nlen
堆的操作
stl算法、stl算法库、stl排序算法、stl常用算法、stl查找算法,以便于您获取更多的相关知识。

时间: 2024-07-30 14:55:38

仿STL中的堆算法的一个实现的相关文章

浅析STL中的常用算法_C 语言

一.非变异算法 是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理.元素查找.子序列搜索.统计和匹配.非变异算法具有极为广泛的适用性,基本上可应用与各种容器. 1查找容器元素find 它用于查找等于某值的元素.它在迭代器区间[first,last)(闭开区间)上查找等于value值的元素,如果迭代器i所指的元素满足*i=value,则返回迭代器i:未找到满足条件的元素,返回last.函数原型:find( v1.begin(), v1.end(), num_to_find ); 复制代码

STL中qsort的七种用法

qsort()  应该就是用的快排.貌似是以数据块的方式移动数据,速度较快. 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); 解释:    qsort ( 数组名 ,元素个数,元素占用的空间(sizeof),比较函数) 比较函数是一个自己写的函数  遵循 int com(const void *a,const void *b) 的格式.当a b关系为 >  <  =

C++ STL中的sort排序算法

问题描述 C++ STL中的sort排序算法 #define _CRT_SECURE_NO_WARNINGS #include"iostream" using namespace std; #include"vector" #include"list" #include"set" #include"algorithm" #include"functional" class teacher

studio-C++ STL 中 有没有B树 斐波那契堆

问题描述 C++ STL 中 有没有B树 斐波那契堆 Visual Studio 2013 C++ STL中 有没有B树 斐波那契堆这些结构 如果没有,有没有能过stl中的其它结构快速建一个的方法 ps. 写自己也能写出来,但是觉得面试的时候 自己去写一个再解决问题,时间不够用吧. 所以问问有没有这样的方法. ps2. 红黑树在STL中有吗? 解决方案 红黑树在stl_tree rbtree,有的. 斐波那契堆没听说,自己实现吧. 解决方案二: stl中的map就是用的红黑树实现的.

opencv中的sift算法是不是只能检测一个匹配目标啊?

问题描述 opencv中的sift算法是不是只能检测一个匹配目标啊? 初学sift算法,现在做一个小程序,用opencv里的sift算法匹配目标,现在假设场景中存在多个匹配的目标,但是每次检测好像都只检测到第一个.怎样让它把多个目标都识别出来啊?小白求指导!先谢谢各位大神 解决方案 sift算法是基于图像特征进行匹配的,你可以尝试基于图像区域的一些算法,看你的匹配目标在哪里,将其设定为匹配区域就可以了

STL中Algorithm

来源:没有代码的日子 toupper,tolower地球人都知道 C++ 的 string 没有 toupper ,好在这不是个大问题,因为我们有 STL 算法: string s("heLLo");transform(s.begin(), s.end(), s.begin(), ::toupper);cout << s << endl;transform(s.begin(), s.end(), s.begin(), ::tolower);cout <&l

STL中的sort和unique函数

问题描述 STL中的sort和unique函数 编了一个小程序,对STL算法中的sort()和unique()进行了测试,发现unique()输出结果不正确.程序如下: #include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int array[8]={1,5,4,5,3,100,2,100}; vector<int> ivec(

用STL的map和算法的find_if实现名表表示和查找功能

问题描述 用STL的map和算法的find_if实现名表表示和查找功能 #include #include #include #include using namespace std; bool older_than_20(int birthdate) { return (2016 - birthdate / 10000)>20; } int main() { maptable_item;//创建一个map类容器,用于存储名表 //创建名表 table_item["Charles"

stl函数对象和算法的区别

问题描述 stl函数对象和算法的区别 函数对象和算法都是函数,有什么区别,我知道的区别是函数对象主要处理一个元素,而算法主要处理区间实质是循环语句,请问它们本质区别是什么? 解决方案 算法是提供了一系列运算功能,比如sort排序,find查找等.而函数对象是一个对象,在算法函数中,比如sort等,它能够支持函数对象作为一个参数来提供自己定制化的功能,比如sort可以使用你函数对象自己定义的比较的逻辑. 解决方案二: 算法是解决问题的方法,和函数没有关系.算法抽象独立于语言,而函数是编程语言的一个