MATLAB Answers

What kind of sorting algorithm is this?

35 views (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

David Goodmanson
David Goodmanson on 11 Dec 2020
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by