I have a discussion with a teacher. He argues that the following algorithm corresponds to the bubble sort but I insist that it is not. Who is right?
clc
clear
a=[0.2 4.333 1/3 5 7]
n=length(a)
for j=n:-1:1
for i=1:j-1
if a(j)>a(i)
else
c=a(i);
a(i)=a(j);
a(j)=c;
a
end
end
end

 Respuesta aceptada

David Goodmanson
David Goodmanson el 11 de Dic. de 2020

1 voto

Hello Karen,
I guess it could depend on what is meant by 'corresponds to', since this sort is O(n^2) as is the bubble sort. So they correspond in that way.
In a bubble sort, only adjacent elements are swapped. That is not happening here. I believe this is a selection sort and that you are correct.

Más respuestas (0)

Categorías

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by