Factorial
# Recursive implementation of n! (n-factorial) calculation
def factorial(n):
# Base case: n = 0 or 1
if n <= 1:
return 1
# Recursive case: n! = n * (n - 1)!
return n * factorial(n - 1)
Fibonacci Sequence
# Recursive implementation to calculate the n-th Fibonacci number
def fibonacci(n):
# Base case: n = 0 or 1
if n <= 1:
return n
# Recursive case: fib(n) = fib(n - 1) + fib(n - 2)
return fibonacci(n - 1) + fibonacci(n - 2)
Sorting:
# Python implementation of Insertion Sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
j = i - 1
while j >= 0 and arr[j + 1] < arr[j]:
# arr[j] and arr[j + 1] are out of order so swap them
tmp = arr[j + 1]
arr[j + 1] = arr[j]
arr[j] = tmp
j -= 1
return arr
# Implementation of MergeSort
def mergeSort(arr, s, e):
if e - s + 1 <= 1:
return arr
# The middle index of the array
m = (s + e) // 2
# Sort the left half
mergeSort(arr, s, m)
# Sort the right half
mergeSort(arr, m + 1, e)
# Merge sorted halfs
merge(arr, s, m, e)
return arr
# Merge in-place
def merge(arr, s, m, e):
# Copy the sorted left & right halfs to temp arrays
L = arr[s: m + 1]
R = arr[m + 1: e + 1]
i = 0 # index for L
j = 0 # index for R
k = s # index for arr
# Merge the two sorted halfs into the original array
while i < len(L) and j < len(R):
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# One of the halfs will have elements remaining
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
# Implementation of QuickSort
def quickSort(arr: list[int], s: int, e: int) -> list[int]:
if e - s + 1 <= 1:
return arr
pivot = arr[e]
left = s # pointer for left side
# Partition: elements smaller than pivot on left side
for i in range(s, e):
if arr[i] < pivot:
tmp = arr[left]
arr[left] = arr[i]
arr[i] = tmp
left += 1
# Move pivot in-between left & right sides
arr[e] = arr[left]
arr[left] = pivot
# Quick sort left side
quickSort(arr, s, left - 1)
# Quick sort right side
quickSort(arr, left + 1, e)
return arr
def bucketSort(arr):
# Assuming arr only contains 0, 1 or 2
counts = [0, 0, 0]
# Count the quantity of each val in arr
for n in arr:
counts[n] += 1
# Fill each bucket in the original array
i = 0
for n in range(len(counts)):
for j in range(counts[n]):
arr[i] = n
i += 1
return arr
arr = [1, 3, 3, 4, 5, 6, 7, 8]
# Python implementation of Binary Search
def binarySearch(arr, target):
L, R = 0, len(arr) - 1
while L <= R:
mid = (L + R) // 2
if target > arr[mid]:
L = mid + 1
elif target < arr[mid]:
R = mid - 1
else:
return mid
return -1
# low = 1, high = 100
# Binary search on some range of values
def binarySearch(low, high):
while low <= high:
mid = (low + high) // 2
if isCorrect(mid) > 0:
high = mid - 1
elif isCorrect(mid) < 0:
low = mid + 1
else:
return mid
return -1
# Return 1 if n is too big, -1 if too small, 0 if correct
def isCorrect(n):
if n > 10:
return 1
elif n < 10:
return -1
else:
return 0
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def search(root, target):
if not root:
return False
if target > root.val:
return search(root.right, target)
elif target < root.val:
return search(root.left, target)
else:
return True