Tricky Algo Analysis - Recursion

Опубликовано: 13 Январь 2024
на канале: Red Black Tech
2,965
77

It's easy to forget about the process call stack and each of the stack frames generated when running a recursive function, because you can't easily see the process call stack.

Unfortunately, this makes it easy to incorrectly analyze the space usage of an algorithm.

In this example, we show how to program the factorial function recursively so that the process stack grows, causing extra space to be in O(n).

I also programmed the function to have what seems to be an extra parameter: accumulator. Why did I do this? Because it allows for the c++ compiler to optimize for Tail Recursion.

When a function is Tail Recursive, it means the recursive call comes last in the function, so that there is no more work after recursion. If there is no more work after recursion, then there's no need to even add to the process stack because we know we will never need the variables in the stack again.


На этой странице сайта вы можете посмотреть видео онлайн Tricky Algo Analysis - Recursion длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Red Black Tech 13 Январь 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 2,965 раз и оно понравилось 77 зрителям. Приятного просмотра!