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 ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by