recursion(mergeSort);

(Posting a day early)
I skipped posting last week for various reasons, one of which was going through the material on recursion a lot slower than I expected, not being aware of how difficult it might be to get my head around it.
THINGS I’VE WORKED ON/COMPLETED…
Recursive methods
I finished Fibonacci (iterative and recursive)
I finished Merge sort (recursion)
ISSUES I HAD…
RECURSION GENERALLY
Recursion was hard. As with many topics on the course, when you look back on them, you still don’t feel like you are an expert, but they never look quite so bad looking back, which I guess is an improvement.
So I tried to follow all the usually recommended steps to solve the recursive problems/project which are basically to:
- find the base case
- to identify a ‘sub-problem’ (smaller ‘problem within the problem’) which is what you can use the recursive call on, to break it down further.
When learning recursion, it’s often mentioned that you should just assume that the recursive part is ‘doing its job’ to avoid confusing yourself by trying to mentally calculate return values as functions are ‘unwound’ or ‘popped off the call stack’. It even amazes me to use language like that as this totally baffled me just two weeks ago. I definitely did not understand what was happening with the call stack initially and even though it still confuses me at times, I understand it a little better.
I found it much easier to trust that the recursive part was ‘doing its job’ when the problems were more basic, but as they became more difficult (as in merge sort) – I thought I should be able to magically anticipate all the return values. To cut a long story short, I could not. I hope that I will one day be able to trust the structure of my recursive functions enough to not have to mentally question return values all the time.
SCOPE AND DIFFERENT FUNCTION CALLS
For one reason or another, I didn’t quite pick up that each recursive call had its own scope and could create new variables (with the same name as variables in other recursive calls). Googling showed me that I am not the first to not quite get this initially, even though it makes perfect sense now.
RETURN VALUES AND VARIABLES
Initially in my recursive functions, I was immediately jumping to set up variables, not understanding how I could use the returned values from recursive calls to ‘update’ variable values.
This still catches me at times, especially in more complex examples. but I do understand it better than I did before.
MERGE SORT
In spite of following all the learning material, I actually think some of it confused me. For one, as I progressed through the project (after having had to ask for a little help) one thing that seemed very noticeable to me was that the mergeSort function itself was really only splitting arrays, even though within it, it called on a separate function ‘merge’, which then sorted and merged the arrays. I don’t know if anyone else has found this, but I think this distorted my understanding how how it needed to be done to some degree – maybe I was just not thinking about it in the right way. In any case, once this became clearer, I found it easier to approach. Just mentioning this in case anyone else might have been confused by this also.
THINGS THAT WENT WELL…
Finished the recursion projects.
Understanding how to ‘update variables’ in recursive calls using return values.
Understanding how to use the return values from recursive calls by assigning them to variables.
THINGS I’VE LEARNT/NEED TO IMPROVE ON…
I think that I’ve covered the things I’ve learnt above, but I would like to improve on recursion generally. I wanted to go off and used some other resources to do this, but I was advised that this is not necessary.
OTHER…
n/a
PLAN FOR THE UPCOMING WEEK…
At a minimum, I want to complete:
Time complexity
Space complexity
Common data structures and algorithms
(Maybe even start the Linked List project, depending on how things go)
Subscribe to my newsletter
Read articles from CodeCara directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
