2025-09-08
O(n²)
A recursive action!
Iterative [loop] solution
:Recursive solution
:Big-O notation
) of these algorithms?What's More?
Feature | Iterative Approach (for loop) | Recursive Approach |
---|---|---|
Time Complexity | O(n) | O(n) |
Space Complexity | O(1) | O(n) |
Readability | Generally considered more straightforward and easier for a new programmer to follow. | Can be more elegant and concise for certain problems, but may be harder to reason about for a simple task. |
Performance | Typically faster. Avoids the overhead of function calls. | Slower due to the overhead of pushing and popping function call frames onto the call stack. |
Memory Usage | Very efficient. Uses a constant amount of memory for the loop counter. | Less memory efficient. Each recursive call adds a new stack frame, potentially leading to a “Stack Overflow” error for very large values of n. |
Facotrial (n!)
n! (n factorial) is n * (n-1) * (n-2) * (n-3) … * 1
“I don’t know how to compute n!…
recursiveMax
takes a not-sorted list of numbers as input, and recursively finds and returns the largest number in the listrecursiveFind
takes two inputs: a number x to search for, and a not-sorted list of numbers nums to search within, and recursively finds and returns the index of the first instance of x within nums.reverseString
takes a string as input, and using recursion returns the reverse of the string.isPrime
takes an int as input, and recursively determines if the number is prime, return True if the number is prime, and False otherwise.