In: Computer Science
One of your chapters this week is on recursion. Below are the three recursive algorithms laws:
1. A recursive algorithm must have a base case.
2. A recursive algorithm must change its state and move toward the base case.
3. A recursive algorithm must call itself, recursively.
Do you have any examples of recursive algorithms used in the real-world?
Approximately 175 words in the answer.
Recursion is a programming technique in which we use function or procedures or algorithm which calls itself in a step which has a termination condition or base case so that successive repetitions are processed up to the critical step where the conditions are met. The main advantages of using recursion are it reduces the time complexity and lesser lines of code when compared to the iterative method. There are many real-world cases where the recursive algorithm is used.
For example, consider you in any application you open a folder which may or may not contain files and we use recursion here in following way. Here first we call the recursion function with folder name to open. Then the recursive function base case checks the condition that folder is empty or not. If it is empty it breaks the recursive function chain is terminated and stop. If the base condition is not met then we read the file popped out the file from folder shows it to the user and call the recursive function again. This process continues and the user gets all the files present in the folder. Once all files are read the base case will halt the recursion program. we can see this example in every application in which we read anything from folder memory. Such as getting the songs name in a folder in windows application or phone application.
I hope you got the answer and understand it.
Thank you:):)