matlab 堆排序
使用matlab写了一下简单的堆排序:
function pilesort
vetor=randi(20000,[1,10000]);
tic
len=length(vetor);
layer_num=floor(log(len)/log(2));
for i=layer_num:-1:1for j=1:2^(layer_num-1)point=[i,j];bool=changevector(point);leaves=getleaf(point);while(bool) point=leaves(bool,:);leaves=getleaf(point);bool=changevector(point);endendend
disp(vetor)
newsort=pile_sort_out();
disp(newsort)function newsort=pile_sort_out(~,~)turn=1;newsort=zeros(1,len);while(len>1)newsort(turn)=vetor(1);vetor(1)=vetor(end);vetor(end)=[];len=len-1;point=[1,1];bool=changevector(point);leaves=getleaf(point);while(bool) point=leaves(bool,:);leaves=getleaf(point);bool=changevector(point);endturn=turn+1;endnewsort(turn)=vetor(1);endfunction bool=changevector(point)leaves=getleaf(point);thepoint=two2one(point);theleaves=[two2one(leaves(1,:));two2one(leaves(2,:))];control=(theleaves<=len);bool=0; if control(1)&&~control(2)if vetor(theleaves(1))<vetor(thepoint)vetor([thepoint,theleaves(1)])=vetor([theleaves(1),thepoint]);bool=1;endend if control(2)&&~control(1)if vetor(theleaves(2))<vetor(thepoint)vetor([thepoint,theleaves(2)])=vetor([theleaves(2),thepoint]);bool=2;endendif control(2)&&control(1)if (vetor(theleaves(2))<vetor(thepoint))||(vetor(theleaves(1))<vetor(thepoint))if vetor(theleaves(1))<vetor(theleaves(2))vetor([thepoint,theleaves(1)])=vetor([theleaves(1),thepoint]);bool=1;elsevetor([thepoint,theleaves(2)])=vetor([theleaves(2),thepoint]);bool=2;endendendendfunction leaves=getleaf(point),leaves=[point(1)+1,point(2)*2-1;point(1)+1,point(2)*2];endfunction branch=getbranch(point),branch=[point(1)-1,ceil(point(2)/2)];endfunction thepoint=two2one(point),thepoint=2^(point(1)-1)-1+point(2);end
toc
end
matlab 堆排序相关推荐
- 堆排序matlab,matlab 堆排序 ...原创(初来报到)
初次来到论坛,用个小程序打个招呼.:D 我用MATLAB写程序时,希望用到较少的类C的循环.程序引用MATLAB库的函数尽量少些.我在搜索某个算法的时候看到论坛.大段程序就不贴了. 于是拿个练习的小程 ...
- 蓝桥杯实战应用【算法代码篇】-堆排序(附Java、python、C++、C语言和matlab代码)
目录 前言 几个相关概念 堆 维护堆 建堆 大顶堆 小顶堆 堆的存储
- Python如何调用matlab函数?
仅需一步就能搭建好两者的通道! 在matlab的命令行下键入: cd (fullfile(matlabroot,'extern','engines','python')) system('python ...
- 利用NVIDIA-NGC中的MATLAB容器加速语义分割
利用NVIDIA-NGC中的MATLAB容器加速语义分割 Speeding Up Semantic Segmentation Using MATLAB Container from NVIDIA NG ...
- 用matlab怎么画视电阻率拟断面图,在MATLAB平台上实现可控源音频大地电磁反演数据三维可视化显示...
第29卷 增刊 物探化探计算技术 2007年10月 收稿日期6文章编号:1001-1749(2007)增刊(1)-0068-04 在MAT LAB 平台上实现可控源音频大地 电磁反演数据三维可视化显示 ...
- 直接法 matlab,解线性方程组直接方法matlab用法.doc
解线性方程组直接方法matlab用法 在这章中我们要学习线性方程组的直接法,特别是适合用数学软件在计算机上求解的方法. 2.1 方程组的逆矩阵解法及其MATLAB程序 2.1.3 线性方程组有解的判定 ...
- matlab编程实现基于密度的聚类(DBSCAN)
1. DBSCAN聚类的基本原理 详细原理可以参考链接: https://www.cnblogs.com/pinard/p/6208966.html 这是找到的相对很详细的介绍了,此链接基本仍是周志华 ...
- matlab编程实现k_means聚类(k均值聚类)
1. 聚类的定义 以下内容摘抄自周志华<机器学习> 根据训练数据是否拥有标记信息,机器学习任务可以大致分为两大类:"监督学习"(supervised learning) ...
- matlab生成多组多维高斯分布数据
matlab生成多组多维高斯分布数据 之所以写这么一个函数,是因为在练习用matlab实现聚类分析,用matlab生成的高斯分布数据可以作为很好的数据.当然,直接load进鸢尾花数据集也可以拿来练手, ...
最新文章
- Linux下系统性能检测利器dstat
- string数组怎么定义
- Win64 驱动内核编程-21.DKOM隐藏和保护进程
- 【Android 逆向】应用数据目录 ( files 数据目录 | lib 应用自带 so 动态库目录 | databases sqlite3 数据库目录 | cache 缓存目录 )
- 【C语言笔记进阶篇】第一章:指针进阶
- python中dtypes_Dataframe创建及index,columns,values,dtypes等属性介绍
- spss聚类分析_SPSS实践:常用聚类分析方法详解
- python字符串查找的四种方法_Python中的字符串查找操作方法总结
- Transformer、BERT细节基础知识点
- 《物联篮球计分器》Android应用程序开发-期末课程设计
- 前IBM人工智能科学家为你解读AI行业的三大核心素养
- 详解会员积分营销系统的作用
- Axure软件页面介绍
- 解决:ORA-06502: PL/SQL: numeric or value error: NULL index table key value
- SLAM--LSD_SLAM在高版本系统中运行(ubuntu20.04 ROS-noetic)
- 心路历程(七)-加入微医集团
- 1276. 不浪费原料的汉堡制作方案(Medium)
- java疯神榜2_FC封神榜获得筋斗云然后用了 怎么下来啊
- 个性化推荐的一些挑战(可能比较老了)
- 找准人生方向 生活才有意义