Effacer les filtres
Effacer les filtres

Counting the number of steps taken by the sorting algorithm

1 vue (au cours des 30 derniers jours)
arfnhtylz
arfnhtylz le 26 Mar 2017
Modifié(e) : Stephen23 le 27 Mar 2017
Hi, I have a code that sorts the elements by their values as an Assignment, but I also need to specify the number of steps that were taken by the code.I searched everywhere and couldn't find any answers Could somebody help me? Here is the code:
X = [9 2 5 7 3 8 1 4 6 0 -2 -7];
n = length(X);
for i=1:n-1
A=X(i);
for j=i+1:n;
if X(j)>A;
A=X(j);
X(j)=X(i);
X(i)=A;
end
end
end
X
Thank you in advance:)

Réponse acceptée

Joshua
Joshua le 27 Mar 2017
X = [9 2 5 7 3 8 1 4 6 0 -2 -7];
n = length(X);
numSteps=0;
for i=1:n-1
A=X(i);
for j=i+1:n;
if X(j)>A;
A=X(j);
X(j)=X(i);
X(i)=A;
numSteps=numSteps+1;
end
end
end
X
numSteps

Plus de réponses (0)

Catégories

En savoir plus sur Price and Analyze Financial Instruments 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