Algorithm Analysis
Algorithm Analysis
Insertion Sort
Sorting
• Arrange an unordered list of elements in some
order.
• Some common algorithms
• Bubble Sort
• Insertion Sort
• Merge Sort
• Quick Sort
Sorting
• Important primitive
• For today, we’ll pretend all elements are distinct.
6 4 3 8 1 5 2 7
1 2 3 4 5 6 7 8
6 4 3 8 5
4 6 3 8 5
Insertion Sort 6 4 3 8 5
example
Insertion-Sort(A, n)
for i = 1 to n – 1
Then move A[2]:
key = A[i] key = 3
j = i – 1
while j >= 0 and A[j] > key
A[j + 1] = A[j] 4 6 3 8 5
j = j – 1
A[j + 1] = key
4 6 6 8 5
4 4 6 8 5
3 4 6 8 5
Insertion Sort 6 4 3 8 5
example
Insertion-Sort(A, n)
for i = 1 to n – 1
Then move A[3]:
key = A[i] key = 8
j = i – 1
while j >= 0 and A[j] > key
A[j + 1] = A[j]
j = j – 1
A[j + 1] = key 3 4 6 8 5
3 4 6 8 5
Insertion Sort 6 4 3 8 5
example
Insertion-Sort(A, n)
for i = 1 to n – 1
Then move A[4]:
key = A[i] key = 5
j = i – 1
while j >= 0 and A[j] > key
A[j + 1] = A[j]
j = j – 1 3 4 6 8 5
A[j + 1] = key
3 4 6 8 8
3 4 6 6 8
3 4 5 6 8
Insertion Sort 6 4 3 8 5
example
Start by moving A[1] toward
the beginning of the list until
you find something smaller
(or can’t go any further): Then move A[3]:
6 4 3 8 5 3 4 6 8 5
4 6 3 8 5 3 4 6 8 5
Then move A[2]: Then move A[4]:
4 6 3 8 5 3 4 6 8 5
3 4 6 8 5 3 4 5 6 8
Then we are done!
Why does this work?
Proof By
Induction!
Outline of a proof by induction
Let A be a list of length n
• Base case:
• A[:1] is sorted at the end of the 0’th iteration. ✓
• Inductive Hypothesis:
• A[:i+1] is sorted at the end of the ith iteration (of the outer loop).
• Inductive step:
• For any 0 < k < n, if the inductive hypothesis holds for i=k-1, then it holds
for i=k.
• Aka, if A[:k] is sorted at step k-1, then A[:k+1] is sorted at step k
(previous slide)
• Conclusion:
• The inductive hypothesis holds for i = 0, 1, …, n-1.
• In particular, it holds for i=n-1.
• At the end of the n-1’st iteration (aka, at the end of the algorithm), A[:n] =
A is sorted.
• That’s what we wanted! ✓
Algorithm Analysis
• Estimate the resources required by an algorithm
• Memory
• Communication Bandwidth
• Energy Consumption
• Computational Time
• Input Size
• Problem specific
• For sorting, the number of items in the input
• For multiplication, the total number of bits needed for
representation
• For graph, the number of nodes and edges
Algorithm Analysis (Insertion Sort)
• Running time of an algorithm
• For a given input
• The number of instructions and data access executed
• Assumption
• Constant time taken by each line of the pseudocode
Algorithm Analysis (Insertion Sort)
• Best Case
• The array is sorted
• 𝑡𝑖 = 1 for all 𝑖 = 2, 3, … , 𝑛
Algorithm Analysis (Insertion Sort)
• Best Case
• The array is sorted A linear function of n
• 𝑡𝑖 = 1 for all 𝑖 = 2, 3, … , 𝑛
𝑇 𝑛 = 𝑎𝑛 + 𝑏
where a = c1 + c2 + c4 + c5 + c8
and b = −(c2 + c4 + c5 + c8 )
Algorithm Analysis (Insertion Sort)
• Worst Case
• The array is sorted in reverse order
• 𝑡𝑖 = 𝑖 for all 𝑖 = 2, 3, … , 𝑛
Algorithm Analysis (Insertion Sort)
• Best Case
• The array is sorted
A quadratic function of n
• 𝑡𝑖 = 𝑖 for all 𝑖 = 2, 3, … , 𝑛
𝑇 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐
where a, b and 𝑐 are constants
Algorithm Analysis (Insertion Sort)
• Instead of exact function, we estimate the rate of
growth or the order of growth
• Best Case
• Most significant term 𝑎𝑛
• Linear
• Worst Case
• Most significant term 𝑎𝑛2
• Quadratic
More on this later