Recursive Searching and Sorting - AP Computer Science A
- What is tail recursion?
- A recursion where the recursive call is the last operation. Can be optimized by compilers into iterative loops to save stack space.
- What happens in stack unwinding in recursion?
- The call stack is cleared as function calls return in reverse order. Functions complete in reverse order of how they were called.
- What is a common application of recursion in search problems?
- Binary search. Efficiently searches sorted arrays by recursively eliminating half the elements.
- State the time complexity of recursive merge sort.
- . Divides array in half, sorts recursively, then merges sorted halves.
- What is the best case time complexity of recursive quicksort?
- . When pivot consistently divides array into balanced partitions.
- What is the time complexity of recursive binary search?
- . Eliminates half the search space with each recursive call.
- What is the base case for a recursive Fibonacci sequence?
- , . These are the first two Fibonacci numbers that stop the recursive sequence.
- What is recursion in computer science?
- Recursion is a method where the solution to a problem depends on solutions to smaller instances. This defines the fundamental principle of breaking down complex problems into simpler versions.
- What is a stack overflow in recursion?
- A stack overflow occurs when there are too many nested recursive calls. Happens when recursion depth exceeds the call stack's memory limit.
- What is the base case for a factorial function?
- , returning 1. By definition, and this stops the recursive multiplication.
- Find the result of factorial(3) using recursive definition.
- 6.
- What is the time complexity of recursive Fibonacci?
- . Each call branches into two more calls, creating exponential growth.
- Find the result of a recursive binary search for 3 in [1, 2, 3, 4].
- Found at index 2. Binary search finds 3 by comparing with middle elements recursively.
- State the purpose of memoization in recursion.
- Memoization stores results of expensive function calls to avoid redundant calculations. Converts exponential time algorithms into linear time by caching results.
- What is a recursive call?
- A function call where the function calls itself with modified arguments. This creates the recursive loop that reduces problem size with each iteration.
- What is a recursive algorithm?
- An algorithm that solves a problem by solving smaller instances recursively. Uses the divide-and-conquer principle by calling itself on smaller inputs.
- What is the time complexity of binary search using recursion?
- . Halves the search space with each recursive call.
- What is the significance of the call stack in recursion?
- Holds information about active subroutines of a computer program. Tracks nested function calls and manages local variables for each recursion level.
- Describe the role of a base case in recursion.
- It prevents infinite recursion by stopping further recursive calls. Essential terminating condition that provides a direct answer without further recursion.
- What is the iterative alternative to recursion?
- Loops (such as for, while). Uses explicit loops instead of function calls to avoid stack overhead.
- Which sort algorithm uses recursion?
- Merge sort. Divides arrays recursively, then merges sorted subarrays back together.
- What is a recursive data structure?
- A data structure that contains instances of itself. Examples include linked lists, trees, and graphs with self-similar structures.
- What is the space complexity of recursive quicksort?
- for balanced partitions. Each recursive call adds a new frame to the call stack.
- What is the worst case time complexity of recursive quicksort?
- . Occurs when pivot is always the smallest or largest element.
- What is mutual recursion?
- Two or more functions that call each other. Creates indirect recursion where functions depend on each other cyclically.
- What is the key advantage of using recursion?
- Simplifies code for problems that can be divided into similar sub-problems. Natural fit for problems with recursive structure like trees and mathematical sequences.
- Find the result of fib(4) using recursive definition.
- 3.
- What is the space complexity of recursive Fibonacci with memoization?
- . Stores computed values to avoid recalculating the same Fibonacci numbers.