Write a function mydsort that sorts a vector in descending order (using a loop, not the built-in sort function)
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:8| Question number:30.8
All Answers
total answers (1)
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