unknown code- can anyone help me with what this code does?
Show older comments
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)
Accepted Answer
More Answers (1)
John D'Errico
on 15 Mar 2020
Edited: John D'Errico
on 15 Mar 2020
0 votes
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 Comment
Josh Williams
on 15 Mar 2020
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!