Implementing Recursive and Non
Implementing Recursive and Non
Recursive Version:
Iterative Version:
Growth Rate: O(log2n) The algorithm halves the search space at each step.
Recursive Version:
Iterative Version:
Recursive Version:
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i=j=k=0
Recursive Version:
Iterative Version:
def iterative_bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
Growth Rate: O(n2)– The algorithm compares each element with others, leading to quadratic
time.
Recursive Version:
Iterative Version:
Growth Rate: O(n3) – Three nested loops lead to cubic time complexity.
Recursive Version:
Growth Rate: O(n!) – The number of permutations of n items is n!, leading to factorial time
complexity.
Conclusion
These implementations provide a clear picture of how algorithm complexity can grow from
logarithmic to factorial. Understanding the growth rates is crucial for choosing the right
algorithm for a specific problem, as more complex algorithms can become infeasible for large
inputs.