matlab堆排序

function [ A ] = heapsort( A )
%UNTITLED14 Summary of this function goes here
%   Detailed explanation goes here
A=build_max_heap(A);
[m,n]=size(A);
for i=n:-1:2
    temp=A(1);
    A(1)=A(i);
    A(i)=temp;
    n=n-1;
    A=max_heapify(A,n,1);
end
end


function [ A ] = build_max_heap( A )
%UNTITLED12 Summary of this function goes here
%   Detailed explanation goes here
[m,n]=size(A);
for i=floor(n/2):-1:1
    A=max_heapify(A,n,i);


end

function [ A ] = max_heapify( A,n,i )
%UNTITLED11 Summary of this function goes here
%   Detailed explanation goes here
l=left(i);
r=right(i);
if l<=n&&A(l)>A(i)
    largest=l;
else
    largest=i;
end
if r<=n&&A(r)>A(largest)
    largest=r;
end
if largest~=i
    temp=A(i);
    A(i)=A(largest);
    A(largest)=temp;
    A=max_heapify(A,n,largest);
end

function [ pIndex ] = parent( i )
%UNTITLED8 Summary of this function goes here
%   Detailed explanation goes here
pIndex=floor(i/2);


end

end

function [ lIndex ] = left( i )
%UNTITLED9 Summary of this function goes here
%   Detailed explanation goes here
lIndex=2*i;


end

function [ rIndex ] = right( i )
%UNTITLED10 Summary of this function goes here
%   Detailed explanation goes here
rIndex=2*i+1;


end


阅读更多
文章标签: matlab 堆排序
个人分类: matlab
上一篇matlab快速排序
下一篇三天没出寝室了
想对作者说点什么? 我来说一句

八大排序算法的MATLAB实现

2016年06月16日 7KB 下载

没有更多推荐了,返回首页

关闭
关闭