Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › RECURSION BASICS
For the array [1, 3, 3, 7, 9], the required checks are 1 <= 3, 3 <= 3, 3 <= 7, and 7 <= 9. Each value is no greater than the value immediately after it. Because these comparisons link together, 1 is also no greater than every later value, 3 at index 1 is no greater than every value after it, and so on. The whole array is therefore sorted in non-decreasing order.
Equality is allowed. The comparison 3 <= 3 is true, so duplicate values do not break sorted order. You do not need to compare every value with every later value, because the ordered links between neighboring values already establish the order of the full array.
Does the equal pair at indices 1 and 2, 3 and 3, violate the sorted order of [1, 3, 3, 7, 9]?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Give the call at index i one job: check whether a[i] <= a[i + 1]. If that comparison succeeds, the call advances to i + 1. For [1, 3, 3, 7, 9], the call at i = 0 checks 1 <= 3, i = 1 checks 3 <= 3, i = 2 checks 3 <= 7, and i = 3 checks 7 <= 9.
After the call at i = 3 succeeds, the next call starts at i = 4. Fewer than two elements remain from index 4, so there is no adjacent pair left to check. The base case returns true there, and the function never tries to read a[5].
bool isSorted(int a[], int n, int i) {
if (i >= n - 1) {
return true;
}
if (a[i] > a[i + 1]) {
return false;
}
return isSorted(a, n, i + 1);
}The Boolean result combines the current comparison with the result from the remaining suffix. For the running array, every current comparison is true, so the call at i = 0 depends on i = 1, which depends on i = 2, which depends on i = 3, which depends on the base call at i = 4.
bool isSorted(int a[], int n, int i) {
if (i >= n - 1) {
return true;
}
return (a[i] <= a[i + 1]) && isSorted(a, n, i + 1);
}The && operator short-circuits. If a[i] <= a[i + 1] is false, the complete expression is already false, so the deeper recursive call is not made. If the current comparison is true, recursion continues. When the base case returns true, that true value travels back through all four successful comparisons, making the result for the full array true.
Complete the return expression so it checks the current pair and then the suffix beginning at i + 1.
return (a[i] <= a[i + 1]) && __________;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For [1, 3, 3, 7, 9], the calls use indices 0, 1, 2, 3, and 4, so there are five calls. The first four calls perform one adjacent comparison each, giving four comparisons. The suffix becomes one element shorter at every recursive step.
Let T(k) describe the work for a suffix of k elements. A non-base call checks one pair and makes one call on a suffix of size k - 1, so T(k) = T(k - 1) + Theta(1). Expanding the recurrence gives one constant amount of work for each element in the suffix, which makes the worst-case time complexity O(n).
The calls remain active while deeper calls run. In the running array, five calls are on the stack at the deepest point, and an array of length n can create one call for each index from 0 through n - 1. The auxiliary space complexity is therefore O(n), entirely from the recursive call stack.