unknown code- can anyone help me with what this code does?
Ältere Kommentare anzeigen
clear
r=rand(15,1)
for i=1:15
for ii=1:15
if (r(i)>r(ii))
temp=r(i);
r(i)=r(ii);
r(ii)=temp;
end
end
end
disp(r)
Akzeptierte Antwort
Weitere Antworten (1)
John D'Errico
am 15 Mär. 2020
Bearbeitet: John D'Errico
am 15 Mär. 2020
0 Stimmen
If I had to guess, it looks like a poorly written attempt at a sorting code, to sort a vector of numbers in decreasing order. My guess is it was written by a student, as the swapping scheme seems a bit kludgy, and the overall code looks as if written by a novice. The clear in front is also the common act of a student.
Why do I say poorly written?
- Because it is fully uncommented. Uncommented code is of no value at all. (see below)
- Because it is an inefficient way to perform a sort, making direct comparisons between all elements of the array with all other elements, and even worse, even with themselves too.
The problem is, code written just as code, with no comments as to what it does, and taken completely out of context is typically just a random string of characters. It has as much value as the output from a thousand monkees, banging away at typewriters.
1 Kommentar
Josh Williams
am 15 Mär. 2020
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!