DSAverse
Initializing Sorting Algorithms...
Sorting Algorithm
Binary Tree
Graph Traversal
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Initializing Sorting Algorithms...
Preparing interactive visualizations for optimal learning experience...
Watch how factorial function calls itself recursively and see the call stack in action.
Click Start to begin the factorial visualization
Click Start to see the call stack in action
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n <= 1:
return 1
# Recursive case: n * factorial(n-1)
return n * factorial(n - 1)
# Example usage
result = factorial(5) # Returns 120function factorial(n) {
// Base case
if (n <= 1) {
return 1;
}
// Recursive case
return n * factorial(n - 1);
}
// Example usage
const result = factorial(5); // Returns 120The function makes exactly n recursive calls. Each call performs constant time operations (comparison and multiplication), so total time is proportional to n.
The recursion depth equals n, meaning the call stack grows to size n. Each stack frame stores the parameter and return address.