Q:

Write a function mydsort that sorts a vector in descending order (using a loop, not the built-in sort function)

0

 Write a function mydsort that sorts a vector in descending order (using a loop, not the built-in sort function).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

mydsort.m

function outv = mydsort(vec)

% This function sorts a vector using the selection sort

% Format of call: mydsort(vector)

% Returns the vector sorted in descending order

for i = 1:length(vec)-1

 highind = i;

 for j=i+1:length(vec)

 if vec(j) > vec(highind)

 highind = j;

 end

 end

 % Exchange elements

 temp = vec(i);

 vec(i) = vec(highind);

 vec(highind) = temp;

end

outv = vec;

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now