Salut les gars. Im essayant d'écrire un algorithme pour heapsort dans Matlab. Ça ne fonctionne pas. Le tas construit bien. Le remplissage du vecteur trié ne fonctionne pas. Voici le code et merci!heapsort à Matlab
function [xs,h]= heap(x)
N = length(x);
h = zeros(1,N);
N_h = 0;
for i=1:N
N_h = N_h +1;
child = N_h;
h(child) = x(i);
while child>1
parent = floor(child/2);
if h(child)>h(parent)
tmp = h(parent);
h(parent) = h(child);
h(child) = tmp;
child = parent;
else
break
end
end
end
xs = zeros(1,N);
parent = 1;
for i = N:-1:1
xs(i) = h(1);
h(1) = h(i);
child1 = 2*parent;
child2= 2*parent+1;
if child1 <= i-1
if h(child1)>h(child2)
cchild = child1;
else
cchild = child2;
end
if h(parent) < h(cchild)
tmp = h(parent);
h(parent) = h(child);
h(child) = tmp;
parent = child;
else
break
end
end
end
Définir "ne fonctionne pas" –