Divide and Conquer
Master Theorem
Master theorem. Let a ≥ 1, b ≥ 2, and c ≥ 0 and suppose that T(n) is a function on the non-negative integers that satisfies the recurrence
T(n)=aT(bn​)+Θ(nc)
with T(0) = 0 and T(1) = Θ(1), where n / b means either ⎣n / b⎦ or ⎡n / b⎤. Then,
If c>logb​a, then T(n)=Θ(nc). (Combining cost more than subproblem)
If c=logb​a, then T(n)=Θ(nclogn).
If c<logb​a, then T(n)=Θ(nlogb​a). (Combining cost less than subproblem)
Binary Search
For a sorted array, find the given item
find the mid item, compare it to the target
less than goes to left subarray; hi=mid−1
bigger than goes to right subarray; lo=mid+1
continue util find it at mid or find none
After loop, i>jand will return -1
Divide and conquer happens at the same time, there is no combining, so c=0
b=2 divide
a=1becuase we only consider one subproblem
c==log2​1==0,T(n)=Θ(nclogn)=Θ(logn)
Bisect
Bisect_left
Given an array a sorted nums and an element target , find the index such that all the elements to the left are smaller than target , all the elements to the right and the current element are bigger than or equal to target.
The index returned can be
0 or len(nums)
Find the middle item, compare it to the target, with lo=0,hi=len(nums)
If less than, consider right subarray by lo=mid+1
If bigger than or equal to, consider the left subarray; however since this current element can be the index we are looking for, so we include the mid,hi=mid
After all scanning, return either lo or hi since lo==hi
Bisect_right
Given an array a sorted nums and an element target , find the index such that all the elements to the left are smaller than or equal to target , all the elements to the right and the current element are bigger than target.
The index returned can be
0 or len(nums)
Find the middle item, compare it to the target, with lo=0,hi=len(nums)
If less than or equal to, consider right subarray; however since this current element can be the index we are looking for, so we include the mid,hi=mid
If bigger than, consider the left subarray lo=mid+1
After all scanning, return either lo or hi since lo==hi
Randomized Quicksort
Given an array of n≥2 distinct elements a1​<a2​<…<an​ the probability that ai​ and aj​ are compared during randomized quicksort is j−i+12​
Hint: think about the subarray ai​<a2​<…<aj​, choosing anything other than ai​or aj​will never compare ai​and aj​anymore, because they are split into different branches of the chosen pivot element. To chose either ai​or aj​ the probabilty is j−i+12​.
Proof:
Step 1, n == 2: a1​and a2​will be definitely compared, and the probability is 1.
Step 2, n≥3:
To choose either ai​or aj​is with prob of n2​
To choose one outside ai​...aj​is with prob of nn−(j−i+1)​, then ai​and aj​will be partitioned into the same branch of the chosen pivot element. By induction, that probability of them get compared within that subarray is j−i+12​
Add them together, proof is concluded.
n2​+nn−(j−i+1)​∗j−i+12​=n2​+(1−n(j−i+1)​)∗j−i+12​=n2​+j−i+12​−n2​=j−i+12​
f(n)=log2​n=nc  c=lognlog2​n​=
Last updated