10.1 Recursion
What is recursion?
📌 Recursion is an iterative process where a method calls itself
10.1 Recursion
What is a base case?
📌 Process gets to the point where it can’t be simplified anymore, this is the Base Case
sum(n) = n + sum(n - 1)

10.1 Recursion
Problem:
Given an Array/ArrayList, return the sum of all the values from the beginning to the target index
public int sumArray(int[] nums, int index){
if(index < 0) {
return 0;
}
return nums[index] + sumArray(nums, index - 1);
}
public static void main(String[] args) {
int[] myNums = {1, 2, 3, 4, 5};
System.out.println(sumArray(myNums, myNums.length-1));
}
10.1 Recursion
Question: 1
What is the definition of recursion?
10.1 Recursion
Question: 2
What is the base case is a recursive statement?
10.1 Recursion
Question: 3
Why do we use recursion?
10.1 Recursion
Question: 4
Of the following problems, which one would a recursive function work best with?
10.1 Recursion
Question: 5
Given the following code, how many calls (including the original call) to theprintX method would be made if we called with printX(20, 15); ?
public static void printX(int x, int y) {
System.out.print(x + “ “);
if (x > y) {
printX(x - 1, y);
}
}
1
3
6
10
6
10.2 Recursive Searching
What are the two common recursive practices?
(1) Searches
(2) Sorts
10.2 Recursive Searching
What is linear search?
Linear Searches:
Checks each element in order until the desired value or the end of the array is reached
10.2 Recursive Searching
What is the linear search algorithm?

10.2 Recursive Searching
What is binary search?
Binary Searches:
Find the midpoint of an array and check if the value is greater than or less than the target. Then eliminates half the values and repeats the process.
An array must be sorted for a binary search
10.2 Recursive Searching
What is binary search efficiency and how is it calculated?
Maximum number of iterations needed for a binary search is roughly calculated using a logarithmic function:
log2arraySize
Or think about it in powers of 2.
Given x iterations, you can search 2x times
Ex: Given array of 512, what is the maximum number of iterations will it take to find an item?
log2 512 = 9
10.2 Recursive Searching
What is the algorithm for iteration for binary search code?

10.2 Recursive Searching
What is the algorithm for recursive for binary search code?

10.2 Recursive Searching
What is the difference between iteration and recursive for binary search code?
10.2 Recursive Searching
What is the difference between Linear Search and Binary Search?
Linear Searches: Iteration only eliminated 1 item from our search
Binary Searches: Each iteration eliminates roughly half of our remaining population

10.2 Recursive Searching
Question: 1
Using the binary search algorithm, what is the maximum number of iterations needed to find an element in an array containing 256 elements?
128
256
4
8
16
8
10.2 Recursive Searching
Question: 2
What is the precondition for binary search to work on an array?
10.2 Recursive Searching
Question: 3
For a large dataset (roughly 100,000 items), which search is more efficient to find a value?
10.2 Recursive Searching
Question: 4
Which best describes how a binary search works?
10.2 Recursive Searching
Question: 5
A binary search can be written either with a loop or with a recursive function.
(True or False)
True
10.3 Recursive Sorting
How can you sort data sets for the use of binary search?
10.3 Recursive Sorting
What is merge sort?
🤖 Merge Sort work by dividing a list into parts, then merging it back together in the correct order
