堆排序2.0

来源:互联网 发布:vb.net加载dll 编辑:程序博客网 时间:2024/05/01 08:42
<span style="font-family:Comic Sans MS;font-size:18px;">#include<iostream>#include<vector>#include<math.h>using namespace std;template<typename T>void swap1(T &a,T &b){    auto temp=a;    a=b;    b=temp;}  template<typename T>void duiSort(vector<T> &v){    int m=v.size();//m,容器大小for(m;m>1;m--){int node=m/2;//node,最后一个节点编号,从1开始for(node;node>=1;node--){int r=2*node+1;//r,右子元素存在,其编号if(v[node-1]>v[r-2]) swap1(v[node-1],v[r-2]);if((r<=m)&&(v[node-1]>v[r-1])) swap1(v[node-1],v[r-1]);}swap1(v[0],v[m-1]);}}</span>

0 0