MATLAB Answers

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

22 ビュー (過去 30 日間)
Kevin Junior
Kevin Junior 2013 年 10 月 19 日
コメント済み: Walter Roberson 2013 年 10 月 19 日
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 件のコメント

Walter Roberson
Walter Roberson 2013 年 10 月 19 日
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 2013 年 10 月 19 日
You do not use C or S, so you might as well eliminate them.

サインイン to comment.

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 19 日
編集済み: Azzi Abdelmalek 2013 年 10 月 19 日
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

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by