Can help me create a selection sort function? I understand how it works but I cant come up with a code that works.

2 vues (au cours des 30 derniers jours)
here is what i have
function y = Mekuluselect(x)
n = length(x);
C = 0; S = 0;
for k = 1:n
C = C + 1;
m = k;
if x(k) < x(m)
temp = x(k);
x(k) = x(m);
x(m) = temp;
S = S + 1;
end
end
y = x;
end
  2 commentaires
Walter Roberson
Walter Roberson le 19 Oct 2013
What happens when you try your code? Does it give an error message? Always? Sometimes? Which arrays does it fail on?
Walter Roberson
Walter Roberson le 19 Oct 2013
You do not use C or S, so you might as well eliminate them.

Connectez-vous pour commenter.

Réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 19 Oct 2013
Modifié(e) : Azzi Abdelmalek le 19 Oct 2013
Look at this line
m = k;
if x(k) < x(m)
this expression is always false, which means your code does nothing
Also we can notice that C is not used by your code

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by