2.2.1 programming techniques Flashcards
1
Q
what is recursion?
A
a function that calls on itself
it has a terminating/base case
it also has a general case
2
Q
how do you convert a recursive algorithm to an iterative algorithm?
A
- 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
3
Q
what are the advantages of recursive algorithms?
A
- uses less lines of code
- well suited to certain problems/elegant solution
4
Q
what are some disadvantages of recursive algorithms?
A
- risk of stack overflow
- uses more memory
5
Q
how can you trace through a recursive algorithm?
A
- 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
6
Q
What are some examples of recursive algorithms?
A
- merge sort
- binary tree search
- calculating factorials
7
Q
A