Lecture 3 Insertion Sort
Lecture 3 Insertion Sort
Aimal Rextin
Department of Computing
Insertion Sort (2)
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Insertion Sort (2)
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Is Insertion-Sort correct?
Can we do better?
Complexity of Insertion-Sort
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Complexity of Insertion-Sort
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Best case:
Complexity of Insertion-Sort (2)
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Worst case:
Complexity of Insertion-Sort (2)
Insertion-Sort(A)
1 for i = 2 to length(A)
2 j =i
3 while j > 1 and A[j − 1] > A[j]
4 swap A[j] and A[j − 1]
5 j = j −1
Worst case: the inner loop is executed so that j goes from its
original location to A[2] (start)
◮ what case is this?
Complexity of Insertion-Sort (3)
The worst-case complexity is when the inner loop is executed exactly
j − 1 times, so
T (n) = 1 + 2 + 3 + · · · + j − 1
T (n) = 1 + 2 + 3 + · · · + j − 1
n(n − 1)
T (n) =
2
T (n) = Θ(n2 )
Complexity of Insertion-Sort (3)
The worst-case complexity is when the inner loop is executed exactly
j − 1 times, so
T (n) = 1 + 2 + 3 + · · · + j − 1
n(n − 1)
T (n) =
2
T (n) = Θ(n2 )
T (n) = 1 + 2 + 3 + · · · + j − 1
n(n − 1)
T (n) =
2
T (n) = Θ(n2 )
Steps:
1. Start with the first element as a ”sorted” subarray.
Insertion Sort: How It Works
Goal: Sort the array by building a sorted subarray incrementally.
Analogy: Like sorting a hand of playing cards.
Steps:
1. Start with the first element as a ”sorted” subarray.
2. For each subsequent element (from position 2 to end):
Insertion Sort: How It Works
Goal: Sort the array by building a sorted subarray incrementally.
Analogy: Like sorting a hand of playing cards.
Steps:
1. Start with the first element as a ”sorted” subarray.
2. For each subsequent element (from position 2 to end):
5 3 4 1 2
Example: Inserting 3, then 4, then 1, then 2.
Formal Proof
Mathematical proofs, show why mathematical hypothsis e.g. ( there
are ∞ prime numbers) are true.
Formal Proof
Mathematical proofs, show why mathematical hypothsis e.g. ( there
are ∞ prime numbers) are true.
X
n
n(n + 1)
i=
2
i=1
Proof by Induction (Part 1)
Theorem: For all positive integers n,
X
n
n(n + 1)
i=
2
i=1
X
n
n(n + 1)
i=
2
i=1
X
k+1
(k + 1)(k + 2)
i=
2
i=1
Proof by Induction (Part 3)
Inductive Step:
k(k + 1)
= + (k + 1) (by hypothesis)
2
k(k + 1) + 2(k + 1)
=
2
(k + 1)(k + 2)
=
2
Proof by Induction (Part 3)
Inductive Step:
k(k + 1)
= + (k + 1) (by hypothesis)
2
k(k + 1) + 2(k + 1)
=
2
(k + 1)(k + 2)
=
2
∴ LHS = RHS for n = k + 1 X
Proof by Induction (Part 3)
Inductive Step:
k(k + 1)
= + (k + 1) (by hypothesis)
2
k(k + 1) + 2(k + 1)
=
2
(k + 1)(k + 2)
=
2
∴ LHS = RHS for n = k + 1 X
Conclusion: Formula holds for all n ∈ N
◮ as induction step and base case both are TRUE
Back to Insertion Sort
Correctness: Inductive Argument
1. Base Case:
◮ When processing i = 2, the subarray A[1..1] is trivially sorted.
2. Inductive Step:
◮ Assume A[1..i − 1] is sorted before processing i.
Correctness: Inductive Argument
1. Base Case:
◮ When processing i = 2, the subarray A[1..1] is trivially sorted.
2. Inductive Step:
◮ Assume A[1..i − 1] is sorted before processing i.
◮ Insert A[i] by swapping it leftward until:
2. Inductive Step:
◮ Assume A[1..i − 1] is sorted before processing i.
◮ Insert A[i] by swapping it leftward until:
2. Inductive Step:
◮ Assume A[1..i − 1] is sorted before processing i.
◮ Insert A[i] by swapping it leftward until: