unknown code- can anyone help me with what this code does?

2 views (last 30 days)
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)
  2 Comments
Josh Williams
Josh Williams on 15 Mar 2020
yes, it displays 15 random numbers. i Understand the random matrix but not the code in the middle

Sign in to comment.

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 15 Mar 2020
Edited: Sriram Tadavarty on 15 Mar 2020
Hi Josh,
This code performs sorting in descending order.
r=rand(15,1) % This statement initializes r with 15 random numbers
for i=1:15 % Loop from 1 to number of elements in r
for ii=1:15 % Loop from 1 to number of elements in r
if (r(i)>r(ii)) % Compares the element with all the elements in the array and performs the swap operation
temp=r(i); % Store the larger value in a temporary variable
r(i)=r(ii); % Replace the smaller value in the array with larger value
r(ii)=temp; % Replace the smaller value with the temporary variable
end
end
end
disp(r) % Display the descending order sequence
% For example, consider an array to be of length 4 with values [5 4 6 3]
% In the first loop i = 1
% When i is equal to 1, ii moves from 1 to 4
% Check 1st element (5) with itself (5), it is not larger, so no swap
% Check 1st element (5) with second element (4), so swap, temp = 5, r(1) = 4, r(2) = 5. So, r is now [4 5 6 3]
% Check 1st element (4) with thrid element (6), it is not larger, so no swap
% Check 1st element (4) with the fourth element (3), it is larger, so swap, temp = 4, r(1) = 3, r(4) = 4. So, r is now [3 5 6 4]
% Now i = 2 , r is till now [3 5 6 4]
% When i is equal to 2, ii moves from 1 to 4
% Check 2nd element (5) with first element (3), it is larger, so swap, temp = 5, r(2) = 3, r(1) = 5, So, r is now [5 3 6 4]
% Check 2nd element (3) with second element (3), it is not larger, so no swap
% Check 2nd element (3) with third element (6), it is not larger, so no swap
% Check 2nd element (3) with fourth element (4), it is not larger, so no swap
% Now i = 3, r is now [5 3 6 4]
% When i is equal to 3, ii moves from 1 to 4
% Check 3rd element (6) with first element (5), it is larger, so swap, temp = 6, r(3) = 5, r(1) = 6, So, r is now [6 3 5 4]
% Check 3rd element (5) with second element (3), it is larger, so swap, temp = 5, r(3) = 3, r(2) = 5, So, r is now [6 5 3 4]
% Check 3rd element (3) with third element (3), it is not larger, so no swap
% Check 3rd element (3) with fourth element (4), it is not larger, so no swap
% Now i = 4, r is [6 5 3 4]
% When i is equal to 4, ii moves from 1 to 4
% Check 4th element (4) with first element (6), it is not larger, so no swap
% Check 4th element (4) with second element (5), it is not larger, so no swap
% Check 4th element (4) with third element (3), it is larger, so swap, temp = 4, r(4) = 3, r(3) =4, So, r is [6 5 4 3]
% Check 4th element (3) with fourth element (3), it is not larger, so no swap
% All the loops ran and r is now [6 5 4 3]
% This is displayed with disp function
% The same can be applied for length of 15 elements in the code
Hope this explains what the code does.
Regards,
Sriram

More Answers (1)

John D'Errico
John D'Errico on 15 Mar 2020
Edited: John D'Errico on 15 Mar 2020
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?
  1. Because it is fully uncommented. Uncommented code is of no value at all. (see below)
  2. 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.

Tags

Community Treasure Hunt

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

Start Hunting!