What kind of sorting algorithm is this?
1 次查看(过去 30 天)
显示 更早的评论
Karen Julieth Capataz Castillo
2020-12-11
回答: David Goodmanson
2020-12-11
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
0 个评论
采纳的回答
David Goodmanson
2020-12-11
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!