|
"Jin " <hj_suzhou@hotmail.com> wrote in message <hdqh7a$p60$1@fred.mathworks.com>...
> three functions, min-heapify, buildheap, heapsort in total, please help , thanks
>
>
>
> function [ A ] =heapify(A,i)
> %UNTITLED4 Summary of thisfunction goes here
> %
> Detailed explanation goes here
> l=2*i;
> r=2*i+1;
> if l<=length(A) &&A(l)< A(i)
> min=l;
> else min=i;
> end
> if r<=length(A) &&A(r)<A(min)
> min=r;
> end
> if min ~=i
> temp=A(min);
> A(min)=A(i);
> A(i)=temp;
> heapify(A,min);
> end
> end
>
>
> function [ A ] =buildheap( A )
> %UNTITLED6 Summary of thisfunction goes here
> %
> Detailed explanation goes here
> heapsize=length(A);
> for i=floor(heapsize/2):-1:1
> heapify(A,i);
> end
> end
>
>
> function
> [A]=heapsort( A )
> %UNTITLED7 Summary of thisfunction goes here
> %
> Detailed explanation goes here
> A=buildheap(A);
> heapsize=length(A);
> for i=length(A):-1:2
> temp=A(i);
> A(1)=temp;
> A(i)=A(1);
> heapsize=heapsize-1;
> heapify(A,1);
> end
> end
Jin,
could you specify what makes you think that the code has a bug, perhaps you can show a short example where the result is not in agreement with your expectation.
Another thread which may give you some ideas is located here
http://www.mathworks.com/matlabcentral/newsreader/view_thread/244840
Darren
|