






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to algorithms, focusing on selection sort and big o notation. Learn about the concept of algorithms, their correctness, and efficiency. Discover how to prove the correctness of selection sort and understand the importance of big o notation in evaluating algorithmic running time.
Typology: Study notes
1 / 10
This page cannot be seen from the preview
Don't miss anything!







What’s left??
To find my name in the phone book, scan trough the names in order until you find mine.
To find my name in part of the the phone book, divide it in half, figure out which half contains my name, and find my name in that half.
How many four-letter words can you make with the letters in ABRACADABRA?
Linear search takes O(n) time; binary search takes O(log n) time.
If T (n) ≤ T (n/2) + O(1), then T (n) = O(log n).
Algorithms
An algorithm is a set of unambiguous instructions describing how to transform some input into some output.
Richard Feynman’s problem-solving “algorithm”:
What we need to know about algorithms:
Is it correct?
SELECTIONSORT(A[1 .. n]): for i ← 1 to n for j ← i + 1 to n if A[j] < A[i] tmp ← A[i] A[i] ← A[j] A[j] ← tmp
How do we prove that an algorithm is correct?
What does “correct” actually mean?
Theorem: SELECTIONSORT reorders the elements of any input array A[1 .. n] into increasing order:
A[1] ≤ A[2] ≤ A[3] ≤ · · · ≤ A[n]
In other words, SELECTIONSORT sorts its input.
Lemma: For any input array A[1 .. n] and any integer i, after the ith itera- tion of the outer loop, A[i] is the smallest element of A[i .. n].
SELECTIONSORT(A[1 .. n]): for i ← 1 to n for j ← i + 1 to n if A[j] < A[i] swap A[i] ↔ A[j]
Lemma: For all input arrays A[1 .. n], for all i and j such that 1 ≤ i < j ≤ n, during the ith iteration of the outer loop, after the jth iteration of the inner loop, A[i] is the smallest element of A[i .. j].
Proof: Let A[1 .. n] be an arbitrary input array. Let i and j be arbitrary integers such that 1 ≤ i < j ≤ n. Consider the ith iteration of the outer loop.
Assume that for all k < j, after the kth iteration of the inner loop, A[i] is the smallest element of A[i .. k].
There are two cases to consider: j = i + 1 and j > i + 1.
In both cases, we conclude that after the jth iteration of the inner loop, A[i] is the smallest element of A[i .. j].
Loop invariants
SELECTIONSORT(A[1 .. n]): for i ← 1 to n 〈〈A[1 .. i − 1] is sorted〉〉 for j ← i + 1 to n 〈〈A[i] is smallest in A[i .. j − 1]〉〉 if A[j] < A[i] swap A[i] ↔ A[j] 〈〈A[i] is smallest in A[i .. j]〉〉 〈〈A[i] is smallest in A[i .. n]〉〉 〈〈A[1 .. i] is sorted〉〉 〈〈A[1 .. n] is sorted〉〉
How long does it take?
SELECTIONSORT(A[1 .. n]): for i ← 1 to n for j ← i + 1 to n if A[j] < A[i] swap A[i] ↔ A[j]
How long does it take?
SELECTIONSORT(A[1 .. n]): for i ← 1 to n for j ← i + 1 to n if A[j] < A[i] swap A[i] ↔ A[j]
The computer science answer:
SELECTIONSORT runs in Θ(n^2 ) time.
Next time we’ll see what this actually means.