Professional Documents
Culture Documents
Lecture-03 - Growth of Functions
Lecture-03 - Growth of Functions
CT065-3.5-3
Assessing Algorithmic
Performance And Correctness
Level 3 – Computing (Software Engineering)
Topic and Structure of the
Lesson
1. Running-time of algorithms
2. Kinds of performance analyses
3. Asymptotic Analysis
4. Analysis of Insertion and Merge Sort
5. Correctness of algorithms
6. Group Exercise
- Analysis of Selection Sort
- Q&A on correctness of algorithms
Module Code and Module Title Title of Slides Slide 2 (of 26)
Running Time of Algorithms
• The running time of an algorithm can be
indicative of its efficient implementation (slide
14, Lecture 1)
• For example, the Merge Sort had a faster
running time as n increased than Insertion and
Selection sort.
• Running time depends on the input: an already
sorted sequence is easier to sort!
• We generally seek upper bounds on the running
time, because we want a guarantee that the
algorithm will run within a certain time frame.
Module Code and Module Title Title of Slides Slide 5 (of 26)
Learning Outcomes
Module Code and Module Title Title of Slides Slide 3 (of 26)
Key Terms
1. Asymptote
2. Big O(O)
3. Theta(Θ)
4. Omega(Ω)
Module Code and Module Title Title of Slides Slide 4 (of 26)
Kinds of Performance Analyses
Worse-case: (usually)
• T(n) = maximum time of algorithm on any input
of size n.
Average-case: (sometimes)
• T(n) = expected time of algorithm over all
inputs of size n.
• Need assumption of statistical distribution of
inputs
Best-case: (bogus)
• Cheat with a slow algorithm that works fast on
some input.
Module Code and Module Title Title of Slides Slide 6 (of 26)
Asymptotic Analysis
Module Code and Module Title Title of Slides Slide 16 (of 26)
Correctness of Algorithms
How do we prove that an algorithm is correct?
• Induction
• Loop invariant
• Proof by contradiction
Module Code and Module Title Title of Slides Slide 17 (of 26)
Induction
Module Code and Module Title Title of Slides Slide 20 (of 26)
Correctness of Insertion Sort
for j ← 2 to n do
// Invariant 1: A[1..j-1] is a sorted
// permutation of the original A[1..j-1]
key ← A[j]
i ← j-1
while (i > 0 and A[i] > key) do
// Invariant 2: A[i .. j] are each >= key
A[i+1] ← A[i]
i ← i -1
A[i+1] ← key
Claim:
• At the start of each iteration of the for loop,
the subarray A[1..j −1] consists of the
elements originally in A[1..j −1] and in
sorted order.
Module Code and Module Title Title of Slides Slide 21 (of 26)
Correctness of Insertion Sort
• Proof of Claim:
– initialisation: j = 2
– maintenance: j j +1
– termination: j = n+1
Module Code and Module Title Title of Slides Slide 22 (of 26)
Correctness of Insertion Sort
Insert sort LI:
At start of for loop, keys initially in A[1..j−1] are in
A[1..j−1] and sorted.
Any Questions?
Module Code and Module Title Title of Slides Slide 25 (of 26)
Summary
1. Running-time of algorithms
2. Kinds of performance analyses
3. Asymptotic Analysis
4. Analysis of Insertion and Merge Sort
Module Code and Module Title Title of Slides Slide 26 (of 26)