3. (50 points) We talked about several different sorting algorithms in class. Fo
ID: 3719628 • Letter: 3
Question
3. (50 points) We talked about several different sorting algorithms in class. For this question, you will write a function that receives any row vector as ingut arxumens andoTE: The only built n sorted in ascending order (smallest to greatest) as an gsusias lensth you will need to use and returns the vector (smallest to greatest) as an output argumens fu loops Your nctions you may use for this question are size, numel, and you will need to use nested code must use the inefficient algorithm described here: Iart by creating a new vector that is the same size as the original vector i. Please that value in the first spot in the new vector V. Re search through the original vector to find the smallest value. peat this process until all of the values have been moved from the original vector to the new vector the value from the original vector (this can be done by replacing it with NaN). est your code on the following vector. You must show that you called the function correctly in the command window. 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
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.