4 methods: hash, insert, find, remove
hash: will take in key an convert it into integer
# this is a super simple version thats uniform distribution
# for each character in key:
hashsum += (char_index + key_length)^ char_code
hashsum = hashsum % capacity
return hashsum
# hashsum must be between 0 and length of internal array
insert:
find: retrieve value under certain key
remove:
way of making algo more efficient by storing intermediate results
3 ways
Recursion
def fib(n):
if n == 1 or n == 2:
result = 1
else:
result = fib(n-1) + fib(n-2)
return result
# O(2^n)
Memoization
def fib(n, memo):
if memo[n] != null:
return memo[n]
if n == 1 or n == 2:
result = 1
else:
result = fib(n-1) + fib(n-2)
memo[n] = result
return result
# complexity: T(n) = # calls * t <= (2n + 1) * O(1)
# = O(n)
# t = time to execute call
Bottom-Up
def fib_bottomup(n):
if n == 1 or n == 2:
return 1
bottom_up = new int[n+1]
bottom_up[1] = 1
bottom_up[2] = 1
for i from 3 upto n:
bottom_up[i] = bottom_up[i-1] + bottom_up[i-2]
return bottom_up[i]
##O(n)