I need help arranging a vector using recursion please.

I am working on a function called vecSort that is supposed to arrange a vector from the smallest element to the largest using recursion. I cannot use the 'sort' function or any function similar to it. I have to use the min and/or max functions. This is what I have so far:
function vectorSort(vec)
L=length(vec);
if L<1
vec=[];
elseif L==1
vec=vec;
else i>1;
I have set the terminating condition but I cannot figure out how to get function to call on itself to arrange the vector from the minimum element to the largest element.

3 Comments

Is it intended that there be a second routine to combine two already-sorted vectors?
Note: remember that recursive routines need to return a value.
You cannot sort recursively in any useful way unless you also have a phase for combining already-sorted regions.
function vecSort(vec)
sortedVec=[];
if length(vec)<=1 %terminating statement
sorted_vec=vec;
else
[m p]=min(vec); %find the minimum value and its position
vec=[vec(1:(p-1)),vec((p+1):end)]; %remove from vec
sortedVec=[sortedVec,m]; %add this value to the sortedVec
vectorsorted=[ vecSort()]
end
Walter could you please tell me how I could make this function to call on itself with recursion? I believe there is something I can add to the last line to make it repeat until the orginal vector is empty and the new one is organizedthe way I need it (least to greatest).

Sign in to comment.

 Accepted Answer

Since you do not have a re-combination phase, your "recursive" function will have to be written from a theoretical point of view, just to prove that it can be done, instead of from a practical point of view. You will need to change your strategy into something closer to a bubble sort, and see https://www.mathworks.com/matlabcentral/answers/361333-help-i-need-to-convert-numbers-into-english-words-using-recursion#answer_285821 on how to transform the "for" loops into recursion.
I would never write a recursive sort this way, and I would never recommend teaching about recursion in this way.
If you had a phase designed to merge the results of two vectors that were already sorted then then that can be done usefully and effectively with recursion. But as you appear to be insisting that you do not have such a phase, you will have to use the ugly theoretical transform between for and recursion, just to satisfy your assignment of having used recursion.

5 Comments

Walter, I have figured out a script that works almost like I need it to. I know using recursion for this isn't necessary but the purpose is to make sure I understand how recursion works with a basic script other than the standard fact.m recursion function every tutorial has. Anyways, my script works, the only problem is when I enter a number more than one time in the vector it does not show up in the sorted vector. For example if I enter:
>>vecSort([ 4 5 2 2 1 7)]
ans= [ 1 2 4 5 7]
when I need it to display 2 two times. Also the numbers are further apart than I need them to be. Could you help me with these two problems? Here is my script:
function sorted_vec=vecSort(vec)
i=length(vec);
if i<=1 %terminating statement
sorted_vec=vec;
else
minv=min(vec);
maxv=max(vec);
vec(i-1); %moves the function towards the terminating statement
sorted_vec=[ minv vecSort(vec(vec>minv))];
end
When there are duplicate minv then
sorted_vec=[ minv vecSort(vec(vec>minv))];
is only putting one copy of them into the output.
sorted_vec=[ vec(vec==minv) vecSort(vec(vec>minv))];
Okay it works now. Thanks a lot Walter!
Well, it would fail on NaN. Do you have NaN in your vector, Neha Prajapati ? If so then where do you want them to be sorted relative to the numbers?

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!