>> A=rand(1,8)
A =
0.4505 0.0838 0.2290 0.9133 0.1524 0.8258 0.5383 0.9961
>> B=bubblesort(A)
B =
0.0838 0.1524 0.2290 0.4505 0.5383 0.8258 0.9133 0.9961
Adapun Fungsinya adalah sbb:
function x = bubblesort(x)
n = length(x);
while (n > 0)
nnew = 0;
for i = 2:n
if (x(i) < x(i - 1))
x = swap(x,i,i - 1);
nnew = i;
end
end
n = nnew;
end
end
function x = swap(x,i,j)
val = x(i);
x(i) = x(j);
x(j) = val;
end
Tidak ada komentar:
Posting Komentar