Sorting
Insertion Sort
def insert_sort(m):
for i in range(2, m): # c1 m
key = A[i] # c2 m - 1
# insert A[i] into sorted A[1: i - 1]
j = i - 1. # c1 m - 1
while j > 0 and A[j] > key:
A[j + 1] = A[j]
j -= 1
A[j + 1] = key
T(m)=c1m+c2(m−1)+c4(m−1)+c5∑i=1mti+c6∑i=1mti−1+c7∑i=1mti−1+c8(m−1)
(c1+c2+c4+c5+c8)m−(c2+c4+c5+c8)=am+λ
Worst Case
T(m)=(2c5+2c2+(c1+c2+c4+2c7)m2+(2c5−2c6−2c7+c5)
=am2+λm+c
=θ(m2)
Divide and Conquer
- divide problem into one or more subsequences