Section 8: Algorithms Flashcards
Chapter 44:
What is a Recursive Subroutine?
A subroutine (e.g. function) that calls itself as part of the execution.
Chapter 44:
What are the 3 characteristics of a Recursive Subroutine?
A Stopping Condition or “Base Case” must be included, which allows the Recursive Subroutine to unwind.
If the Stopping Condition is not met, the routine must call itself.
The Stopping Condition must be met in a finite number of calls.
Chapter 44:
How might a Recursive Subroutine calculate the factorial of an integer?
SUB CallFactorial(n) IF n == 0 THEN factorial = 1 ELSE factorial = n * CallFactorial(n-1) ENDIF RETURN factorial ENDSUB
Chapter 44:
What concept do Recursive Algorithms rely on to recall progress from the called line?
Stacks.
When a subroutine is called, the current values are added to a stack for later retrieval.
New values may be assigned (local variables) and some may be carried through to the next layer of the stack (parameters).
Chapter 44:
What are 3 related examples of where Recursion is used?
In-Order, Pre-Order, Post-Order Tree Traversal.
Chapter 44:
How would you construct a Recursive In-Order Tree Traversal in pseudocode?
SUB inOrderTraversal(p) IF tree[p].left != -1 THEN inOrderTraversal( tree[p].left ) ENDIF OUTPUT ( tree[p].data ) IF tree[p].right != -1 THEN inOrderTraversal( tree[p],right ) ENDIF ENDSUB
Chapter 44:
How would you construct a Recursive Pre-Order Tree Traversal in pseudocode?
SUB preOrderTraversal(p) OUTPUT ( tree[p].data ) IF tree[p].left != -1 THEN preOrderTraversal( tree[p].left ) ENDIF IF tree[p].right != -1 THEN preOrderTraversal( tree[p],right ) ENDIF ENDSUB
Chapter 44:
How would you construct a Recursive Post-Order Tree Traversal in pseudocode?
SUB postOrderTraversal(p) IF tree[p].left != -1 THEN postOrderTraversal( tree[p].left ) ENDIF IF tree[p].right != -1 THEN postOrderTraversal( tree[p],right ) ENDIF OUTPUT ( tree[p].data ) ENDSUB
Chapter 45:
What is the name given to how much time an algorithm takes to execute?
Time Complexity.
Chapter 45:
What are the 4 main types of algebraic function?
Linear,
Polynomial,
Exponential,
Logarithmic.
Chapter 45:
What does the ‘O’ in Big-O notation stand for?
Order.
Chapter 45:
What does Big-O notation refer to?
Time Complexity.
Chapter 45:
What does O(1) refer to?
An algorithm that takes constant time with variable inputs. (Always takes the same amount of time).
Chapter 45:
What does O(n) refer to?
An algorithm that takes linear time.
The time taken is directly proportional to the size of the data.
Chapter 45:
What does O(n^2) refer to?
An algorithm that takes polynomial time.
The time taken is directly proportional to the square of the size of the data.
Chapter 45:
What does O(2^n) refer to?
An algorithm that takes exponential time.
The time taken will double with every new item.
Chapter 45:
What does O(log n) refer to?
An algorithm that takes logarithmic time.
The time taken will slowly increase as the size of the data increases.
Chapter 45:
What does O(n!) refer to?
An algorithm that takes factorial time.
The time taken will quickly increase as the size of the data increases.
Chapter 46:
What is Linear Search?
Where you incrementally search through all data spaces (in order), to find what you are looking for.
Chapter 46:
What is it called when you incrementally search through all data spaces (in order), to find what you are looking for?
Linear Search.
Chapter 46:
What is the Time Complexity of Linear Search?
O(n)