• 2.堆排序


    #include "string"
    #include "vector"
    #include "time.h"
    typedef std::basic_string<TCHAR> tstring;
    typedef std::vector<int> IntVec;
    
    // iIndexAdjust 从0计数.... left = 2i+1;right = 2i+2
    // 最后一个非叶子节点  (iHeapTreeSize - 2) / 2
    // iHeapTreeSize 无序堆大小(无序区节点数)
    void HeapifyMax(IntVec & iHeapTree,int const iIndexAdjust,int const iHeapTreeSize)
    {
    	if (iIndexAdjust > (iHeapTreeSize - 2) / 2)//只针对非叶子节点
    		return;
    
    	int iIndexGreater = iIndexAdjust;
    	
    	int child = 2 * iIndexAdjust + 1;
    	if (child < iHeapTreeSize && iHeapTree[child] > iHeapTree[iIndexGreater])
    		iIndexGreater = child;
    
    	child = 2 * iIndexAdjust + 2;
    	if (child < iHeapTreeSize && iHeapTree[child] > iHeapTree[iIndexGreater])
    		iIndexGreater = child;
    
    	if (iIndexAdjust == iIndexGreater)
    		return;
    	std::swap(iHeapTree[iIndexAdjust], iHeapTree[iIndexGreater]);
    
    	HeapifyMax(iHeapTree, iIndexGreater, iHeapTreeSize);//重新修正子树
    
    }
    // 从最后一个非叶子节点开始构建
    void HeapBuild(IntVec & iHeapTree){
    
    	for (int i = (iHeapTree.size() - 2) / 2; i >= 0; i--){
    		HeapifyMax(iHeapTree, i,iHeapTree.size());
    	}
    }
    void HeapSort(IntVec & iHeapTree){
    
    	for (int i = iHeapTree.size() - 1; i >= 0; i--)
    	{
    		std::swap(iHeapTree[0], iHeapTree[i]);
    		HeapifyMax(iHeapTree, 0, i); // 最后一个参数 HeapBuild时是 iHeapTree.size(),交换一次根,少一个节点
    	}
    }
    void HeapPrint(IntVec & iHeapTree){
    
    	for (auto i:iHeapTree){
    		printf("%d ", i);
    	}
    	printf("
    ");
    }
    int _tmain(int argc, _TCHAR* argv[])
    {
    	srand((unsigned int)time(0));
    	IntVec ints;
    	for (int i = 0; i < 20; i++){
    
    		ints.push_back(rand() % 100);
    	}
    	printf("arr init
    ");HeapPrint(ints);
    	
    	HeapBuild(ints);
    	printf("arr after build 
    "); HeapPrint(ints);
    
    	HeapSort(ints);
    
    	printf("arr after sort 
    "); HeapPrint(ints);
    	return 0;
    }
    

      

    arr init
    74 51 18 91 59 88 48 16 76 23 17 64 52 82 30 67 52 5 93 12
    arr after build
    93 91 88 76 59 64 82 67 74 23 17 18 52 48 30 16 52 5 51 12
    arr after sort
    5 12 16 17 18 23 30 48 51 52 52 59 64 67 74 76 82 88 91 93
    

      

  • 相关阅读:
    Model2
    servlet_获取初始化参数
    Java_Reflect反射
    Java Web_过滤器
    JSP状态管理_1_Cookie
    JSP_内置对象_session
    Sql Server中的数据类型和Mysql中的数据类型的对应关系(转)
    组合权限查询 SQL,UniGUI
    BIM 3D 数据交换格式 ----张建平(清华女)
    即速应用_微信小程序开发 http://www.jisuapp.cn/
  • 原文地址:https://www.cnblogs.com/ezhong/p/3981913.html
Copyright © 2020-2023  润新知