Recursion P3
Recursion P3
Popcorn Hack #1
// 5! = 5 * 4! = 5 * (5 – 1)!
// 4! = 4 * 3! = 4 * (4 – 1)!
// ...
// 1! = 1
public static int factorial(int target) {
// Base case: If target is 1 or 0, return 1
if (target == 0 || target == 1) {
return 1;
}
// Recursive step: Multiply target by factorial of (target - 1)
return target * factorial(target - 1);
}
public static void main(String[] args) {
System.out.println(factorial(5));
}
main(null);
120
Popcorn Hack #2
// Pro tip: Think of why the index is stored as a parameter
// What are parameters usually used as?
public static int sumArray(int[] arr, int index) {
// Base case: If index is out of bounds, return 0
if (index == arr.length) {
return 0;
}
// Recursive step: Add the current element to the result of the next element
return arr[index] + sumArray(arr, index + 1);
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(sumArray(numbers, 0));
}
main(null);
15
Popcorn Hack #3
public static void selectionSort(int[] arr, int index) {
// Base case: if the index is at the end of the array, stop recursion
if (index == arr.length - 1) {
return;
}
// Find the index of the smallest element in the subarray starting at 'index'
int minIndex = index;
for (int i = index + 1; i < arr.length; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
// Swap the smallest element with the current element
int temp = arr[index];
arr[index] = arr[minIndex];
arr[minIndex] = temp;
// Recur for the next part of the array
selectionSort(arr, index + 1);
}
public static void main(String[] args) {
int[] numbers = {64, 25, 12, 22, 11};
selectionSort(numbers, 0);
System.out.println(Arrays.toString(numbers));
}
main(null);
[11, 12, 22, 25, 64]
Popcorn Hack #4
public static void sort2DArray(int[][] arr, int x_index, int y_index) {
// Base case: if we reach the last row and column, stop recursion
if (x_index == arr.length - 1 && y_index == arr[0].length) {
return;
}
// Find the smallest element in the 2D array starting from (x_index, y_index)
int minRow = x_index, minCol = y_index;
for (int i = x_index; i < arr.length; i++) {
for (int j = (i == x_index ? y_index : 0); j < arr[0].length; j++) {
if (arr[i][j] < arr[minRow][minCol]) {
minRow = i;
minCol = j;
}
}
}
// Swap the smallest element with the current element
int temp = arr[x_index][y_index];
arr[x_index][y_index] = arr[minRow][minCol];
arr[minRow][minCol] = temp;
// Move to the next element
if (y_index < arr[0].length - 1) {
sort2DArray(arr, x_index, y_index + 1);
} else {
sort2DArray(arr, x_index + 1, 0);
}
}
main(null);
Alice: 23
Charlie: 21
Bob: 19
Homework Hack
public class Student {
String name;
int efficiency, collab, iq, performanceScore;
public Student(String name, int efficiency, int collab, int iq) {
this.name = name;
this.efficiency = efficiency;
this.collab = collab;
this.iq = iq;
this.performanceScore = calculatePerformanceScore();
}
public int calculatePerformanceScore() {
return (collab * 2) + (iq * 3) + efficiency;
}
}
public static void sortStudents(Student[] students, int index) {
// Base case: if the index is at the end of the array, stop recursion
if (index == students.length - 1) {
return;
}
// Find the student with the highest performance score in the subarray
int maxIndex = index;
for (int i = index + 1; i < students.length; i++) {
if (students[i].performanceScore > students[maxIndex].performanceScore) {
maxIndex = i;
}
}
// Swap the current student with the one having the highest performance score
Student temp = students[index];
students[index] = students[maxIndex];
students[maxIndex] = temp;
// Recur for the next part of the array
sortStudents(students, index + 1);
}
public static void main(String[] args) {
Student[] students = {
new Student("Alice", 5, 3, 4),
new Student("Bob", 3, 5, 2),
new Student("Charlie", 4, 4, 3)
};
sortStudents(students, 0);
for (Student student : students) {
System.out.println(student.name + ": " + student.performanceScore);
}
}
main(null);
Alice: 23
Charlie: 21
Bob: 19