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

 채택된 답변

David Goodmanson
David Goodmanson 2020년 12월 11일

1 개 추천

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.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by