1 Insertionsort
1 Insertionsort
CSOR W4246
Eleni Drinea
Computer Science Department
Columbia University
1 Overview
3 Analysis of algorithms
4 Efficiency of algorithms
Today
1 Overview
3 Analysis of algorithms
4 Efficiency of algorithms
Algorithms
1 Overview
3 Analysis of algorithms
4 Efficiency of algorithms
Sorting
Example
I Input: n = 6, A = {9, 3, 2, 6, 8, 5}
Sorting
Example
I Input: n = 6, A = {9, 3, 2, 6, 8, 5}
I Output: A = {2, 3, 5, 6, 8, 9}
Example
I Input: n = 6, A = {9, 3, 2, 6, 8, 5}
I Output: A = {2, 3, 5, 6, 8, 9}
sorted unsorted
key
key
1 i-1 i n
sorted unsorted
key
key
1 i-1 i n
sorted unsorted
key
key
1 i-1 i n
sorted unsorted
key
key
1 i-1 i n
sorted unsorted
key
sorted unsorted
key
sorted unsorted
2 3
9 9 6 8 5 beginning of iteration i=4
key
unsorted
sorted
2 3
9 6 9 8 5 beginning of iteration i=5
key
sorted unsorted
2 3
9 6 8 9 5 beginning of iteration i=6
sorted
2 3
9 5 6 8 9 end of iteration i=6
Pseudocode
insertion-sort(A)
for i = 2 to n do
key = A[i]
//Insert A[i] into the sorted subarray A[1, i − 1]
j =i−1
while j > 0 and A[j] > key do
A[j + 1] = A[j]
j =j−1
end while
A[j + 1] = key
end for
Today
1 Overview
3 Analysis of algorithms
4 Efficiency of algorithms
Analysis of algorithms
I Correctness
I Running time
I Space
Analysis of algorithms
Fact 1.
Pn n(n+1)
For all n ≥ 1, i=1 i = 2 .
Example of induction
Fact 1.
Pn n(n+1)
For all n ≥ 1, i=1 i = 2 .
Proof.
I Base case: n = 1
I Inductive hypothesis: PAssume that the statement is
true for n ≥ 1, that is, ni=1 i = n(n+1)
2 .
I Inductive step: We show that the statement is true for
n + 1. That is, i=1 i = (n+1)(n+2)
Pn+1
2 . (Show this!)
I Conclusion: It follows that the statement is true for all n
since we can apply the inductive step for n = 2, 3, . . ..
Correctness of insertion-sort
Claim 1.
Let n ≥ 1 be a positive integer. For all 1 ≤ i ≤ n, after the i-th
loop, the subarray A[1, i] is sorted.
By induction on i.
...
unexamined
key key
End of i+1-st iteration:
A[1,i+1] is sorted
1 ℓ ℓ+1 ℓ+2 i i+1 n
Running time T (n) of insertion-sort
for i = 2 to n do
key = A[i]
//Insert A[i] into the sorted subarray A[1, i − 1]
j =i−1
while j > 0 and A[j] > key do
A[j + 1] = A[j]
j =j−1
end while
A[j + 1] = key
end for
for i = 2 to n do line 1
key = A[i] line 2
//Insert A[i] into the sorted subarray A[1, i − 1]
j =i−1 line 3
while j > 0 and A[j] > key do line 4
A[j + 1] = A[j] line 5
j =j−1 line 6
end while
A[j + 1] = key line 7
end for
for i = 2 to n do line 1
key = A[i] line 2
//Insert A[i] into the sorted subarray A[1, i − 1]
j =i−1 line 3
while j > 0 and A[j] > key do line 4
A[j + 1] = A[j] line 5
j =j−1 line 6
end while
A[j + 1] = key line 7
end for
Definition 2.
Worst-case running time: largest possible running time of the
algorithm over all inputs of a given size n.
1 Overview
3 Analysis of algorithms
4 Efficiency of algorithms
Efficiency of insertion-sort and the brute force solution
Definition 4.
An algorithm is efficient if it has a polynomial running time.
Caveat
I What about huge constants in front of the leading term or
large exponents?
However
I Small degree polynomial running times exist for most
problems that can be solved in polynomial time.
I Conversely, problems for which no polynomial-time
algorithm is known tend to be very hard in practice.
I So we can distinguish between easy and hard problems.
Remark 1.
Today’s big data: even low degree polynomials might be too slow!
Are we done with sorting?
1. Can we do better?