# 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

08-17 1165

04-20 830

#### 八大<em>排序</em>算法的<em>MATLAB实现</em>

2018年05月07日 00:00

06-09 2905

08-29 541

03-04 710

07-13 8

08-16 830

03-16 107

#### 八大排序算法的MATLAB实现

2016年06月16日 7KB 下载