10 min readβ’june 18, 2024
Kashvi Panjolia
Exam Weighting
Enduring Understanding
Building Computational Thinking
Main Ideas for the Unit π‘
public int factorial(int n) {
if (n == 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
n
as an argument and returns the factorial of n
. The base case is when n
is equal to 1, in which case the function returns 1. For all other values of n
, the function returns n
multiplied by the factorial of n-1
. Notice how the function calls itself inside the else
statement, but modifies the argument being passed into it. This means, the else statement contains the recursive call. Even though there is a return statement inside the else
statement, since the function is calling itself, the method will run again and again until the base case is reached, at which point the recursion stops and the final result is returned.public int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result = result * i;
}
return result;
}
for
loop to iterate over the range of numbers from 1 to n
, and multiplying the result by each number as it goes. The final result is returned after the loop finishes. public void traverseArray(int[] array, int index) {
if (index == array.length) {
return;
}
System.out.println(array[index]);
traverseArray(array, index + 1);
}
public void traverseArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
traverseArray
method looks much simpler than the recursive version, which is why it is more widely used than the recursive version to traverse arrays. However, it is important to understand the recursive method of traversing arrays so you can practice using recursion.low
to the first element of the array and an upper bound high
to the last element of the array.mid
to the average of low
and high
.mid
, return mid
.mid
, set the new high
to mid - 1
and go back to step 2.mid
, set the new low
to mid + 1
and go back to step 2.low
index is greater than the high
index, the target element is not present in the array, and the function returns -1.public int binarySearch(int[] array, int target) {
int low = 0;
int high = array.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (array[mid] == target) {
return mid;
} else if (target < array[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
while
loop to repeatedly check the middle element of the search space and narrow down the search area until the target element is found or it is clear that the element is not present.public int binarySearch(int[] array, int target, int low, int high) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
if (array[mid] == target) {
return mid;
} else if (target < array[mid]) {
return binarySearch(array, target, low, mid - 1);
} else {
return binarySearch(array, target, mid + 1, high);
}
}
low
index is greater than the high
index, in which case the function returns -1 because the lower index should always be less than the higher index. For all other cases, the function checks the middle element, and calls itself with modified bounds to search either the lower half or the upper half of the array depending on whether the target element is less than or greater than the element at mid
.An example of how binary search uses fewer steps than sequential search. Image courtesy of Tenor.
Merge Sort
public void mergeSort(int[] array) {
if (array.length > 1) {
// Split the array in half
int[] left = Arrays.copyOfRange(array, 0, array.length / 2);
int[] right = Arrays.copyOfRange(array, array.length / 2, array.length);
// Sort the halves
mergeSort(left);
mergeSort(right);
// Merge the sorted halves
merge(array, left, right);
}
}
public void merge(int[] result, int[] left, int[] right) {
// Merge the two sorted arrays into the result array
int i = 0;
int j = 0;
int k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result[k] = left[i];
i++;
} else {
result[k] = right[j];
j++;
}
k++;
}
// Copy any remaining elements of the left array
while (i < left.length) {
result[k] = left[i];
i++;
k++;
}
// Copy any remaining elements of the right array
while (j < right.length) {
result[k] = right[j];
j++;
k++;
}
}
left
and right
, and copying the elements of the original array into them. The left
array contains the elements from the beginning of the original array up to (but not including) the middle element, and the right
array contains the elements from the middle element to the end of the original array.mergeSort()
function recursively on each half of the array. This causes the function to keep calling itself with smaller and smaller versions of the original problem until the base case is reached, at which point the recursion stops and the final result is returned.mergeSort()
function is when the array has only one element, in which case it is already sorted and the function ends.merge()
function. This function compares the elements of the two input arrays and inserts the smaller element into the result array. It continues this process until one of the input arrays is exhausted, at which point it copies the remaining elements of the other array into the result array.mergeSort()
function is a sorted version of the original array.A useful GIF visually explaining how merge sort works. Image courtesy of Wikimedia Commons.
Β© 2025 Fiveable Inc. All rights reserved.