堆排序

STL模板,实现的堆排序功能

// range heap example
#include <iostream>
#include <algorithm>   // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>

int main(){
	int myints[] = {10, 20, 30, 5, 15};
    std::vector<int> v(myints, myints + 5);

   std::make_heap(v.begin(), v.end());
   std::cout << "initial max heap:" << v.front() << '\n';
   
   return 0;
}

ocr:
1:资源
tesseract https://github.com/teseract-ocr/tesseract
cppan https://github.com/tesseract-ocr/tessearct/wiki/Compiling#window
cmake https://cmake.org/download
2. 安装cmake,cppan。添加路径到系统环境变量
3. 运行cppan,运行cmake

linux:
编译leptonica

cd leptonica
autoreconf -vi
./autobuild
./configure
make
sudo make install

编译tesseract

cd tesseract
./autogen.sh
./configure
LDFLAGS ="-L/usr/local/lib" CFLAGS="-I/usr/local/include" cmake
sudo make install
sudo ldconfig

#include <iostream>
int g_array[] = {6, 1, 2, 7, 9, 10, 4, 5, 10, 8};

void adjust_heap(int target_array[], int node, int size){
	int left = node * 2 + 1;
	int right = node * 2 + 2;
	int max = node;
     
     if(left < size && target_array[left] > target_array[max]
     {
     	max = left;
     }
     if(right < size && target_array[right] > target_array[max]){
         max = right;
     }
     
     if(max != node){
         swap(target_array[max], target_array[node]);
         adjust_heap(target_array, max, size);
     }
}

void heap_sort(int target_array[], int size){
     for(int i = size/2 - 1; i >= 0;i--){
          adjust_heap(target_array, i, size);
     }

     for(int i = size - 1;i>=0 ; i--){
          swap(target_array[0], target_array[i]);
          adjust_heap(target_array, 0, i);
     }
}

int main(){
    heap_sort(g_array, sizeof(g_array) /sizeof(g_array[0]);
    for(auto item : g_array){
    	   std::cout << item << std::endl;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值