% Function Name: slowSort % Inputs (1): - (double) A 1xN array of scalars % Outp
ID: 3538835 • Letter: #
Question
% Function Name: slowSort
% Inputs (1): - (double) A 1xN array of scalars
% Outputs (1): - (double) The input array sorted in ascending order
%
% Function Description:
%
% Given a 1xN array of scalars (a vector), write a function, slowSort,
% that recursively sorts the vector in ascending order and outputs this
% sorted vector.
%
% Constraints:
% You MUST use recursion to solve this problem
%
% Test Cases:
%
% a = slowSort([13 -4 0 17 25 3 18 48]);
% a => [-4 0 3 13 17 18 25 48]
%
% b = slowSort([86 48 53 -20 -30 4 7 8]);
% b => [-30 -20 4 7 8 48 53 86]
Explanation / Answer
function out=slowSort(A)
% logic:
% find the minimum, store it, delete it from original vector,
% append slowSort of remaining vector to minimum
if max(size(A))==1
out=A;
else
[m i]=min(A);
A(i)=[];
out=[m slowSort(A)];
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.