MATLAB Question 3. (50 points) We talked about several different sorting algorit
ID: 3919831 • Letter: M
Question
MATLAB Question
3. (50 points) We talked about several different sorting algorithms in class. For this question, you write a function that receives any row sorted in ascending order (smallest to greatest) as an vector as ingut argument and returns the vector gutput arsument NOTE: The only built in functions you may use for this question are size, numel, and lengthe yo loops Your code must use the inefficient algorithm described here m. Please that value in the first spot in the new vector. tart by creating a new vector that is the same size as the original vector Now search through the original vector to find the smallest value. IV. Erase the value from the original vector (this can be done by replacing it with NaN). process until al of the values have been moved from the original vector to the new vector Test your code on the the command window. following vector. You must show that you called the function correctly in V-[2 45 1 95 -2 -5 4 19 25 0 16]Explanation / Answer
Save the following code in mySort.m
function output = mySort(L)
output = zeros(1, numel(L));
for p = 1:length(output)
minIndex = 0;
for q = 1:length(L)
if ~isnan(L(q)) && (minIndex == 0 || L(q) < L(minIndex))
minIndex = q;
end
end
output(p) = L(minIndex);
L(minIndex) = NaN;
end
end
Now in command window copy paste the following lines
V =[2 45 1 95 -2 -5 4 19 25 0 16]
mySort(V)
output
V =
2 45 1 95 -2 -5 4 19 25 0 16
ans =
-5 -2 0 1 2 4 16 19 25 45 95
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.