2.2.1 programming techniques
No description yet. This curated deck is ready for daily study.
Card preview (7)
what is recursion?
a function that calls on itself it has a terminating/base case it also has a general case
how do you convert a recursive algorithm to an iterative algorithm?
• the opposite of the base case is used for the while loop condition • the parameters in the recursive calls of the function are set as changing the variables
what are the advantages of recursive algorithms?
• uses less lines of code • well suited to certain problems/elegant solution
what are some disadvantages of recursive algorithms?
• risk of stack overflow • uses more memory
how can you trace through a recursive algorithm?
• use a table (if provided) and write each function call, the value of the parameters and the return • write each line of code and write the decisions (true/false) next to it. Then do this for the next call of the function with the correct values. finally, write the return values of each instance of the function call
What are some examples of recursive algorithms?
• merge sort • binary tree search • calculating factorials