Seminar 2 - Analysis of Algorithms
Seminar 2 - Analysis of Algorithms
Week 2:
Analysis of Algorithms
1
7/03/2025
Outline
Algorithmic Analysis
2
7/03/2025
return min
3
7/03/2025
min = array[1]
index = 2
return min
min = array[1]
index = 2
Will we get a correct
result if we replace
index <= N with
while index <= N
index < N?
return min
4
7/03/2025
5
7/03/2025
6
7/03/2025
• We saw that LI is true for all values of index (in the previous step).
• We have set index to the value which causes termination (N+1).
• Check if the statement of LI with this index value is what we want
//LI: min equals the minimum value in array[1 … N]
• After the last iteration, min will hold the minimum value in the entire
array from array[1] to array[N].
• Thus, the final value of min after the loop ends will be the minimum
value in the range array[1 … N], which is the desired outcome.
1 2 3 4 5 6 7 8 9 10 11
lo mid mid hi
mid
…
7
7/03/2025
while ( lo < hi )
mid = floor( (lo+hi)/2 )
if key >= array[mid] Is this algorithm correct?
lo=mid To prove correctness, we need to
else show that
hi=mid
1. it always terminates, and
2. it returns correct result when it
if N > 0 and array[lo] == key
terminates
print(key found at index lo)
else
print(key not found)
8
7/03/2025
lo hi Key = 13
9
7/03/2025
BinarySearch(L[1..n], t)
lo = 1
Initialisation
hi = N+1
• If rightmost target is in L, then
while ( lo < hi - 1 ) target is in L[lo…hi-1] =
mid = (lo + hi) / 2 L[1…N] = L
if t ≥ L[mid]
lo = mid
if t < L[mid]
hi = mid
if L[lo] == t
return lo
return False
10
7/03/2025
11
7/03/2025
Outline
Comparison-based Sorting
Comparison-based sorting algorithms sort the input array
by comparing the items with each other:
Selection Sort
Insertion Sort
Quick Sort
Merge Sort
Heap Sort
…
12
7/03/2025
Comparison Cost
Typically, we assume that comparing two elements takes O(1), e.g.,
array[i] <= array[j]. This is not necessarily true.
Comparison Cost
Typically assume comparison cost is O(1) (small values)
Sometimes comparison cost is critical: genome sequences may have
millions of characters – expensive comparison.
The cost of comparison-based sorting is often taken as in terms of # of
comparisons, e.g., # of comparisons in merge sort is O(N log N).
This ignores comparison cost (which is mostly fine).
In summary:
In this unit we will generally assume that
String comparison is O(c), where c is the number of characters which get
compared
Numerical comparison is O(1) unless otherwise specified
13
7/03/2025
Output Marks 70 75 75 80 85 90
Name Don Bill Maria Alice Leo Geoff
Outline
14
7/03/2025
2 4 5 6 10 8 7 11
i
FIT2004: Lecture 2 - Analysis of Algorithms
2 4 5 6 10 8 7 11
i j
15
7/03/2025
2 4 5 6 7 8 10 11
i j
2 4 5 6 7 8 10 11
i j
16
7/03/2025
While doing the proof, we need to show that if the LI is true at iteration k, it is true
at iteration k+1
2 4 5 8 10 12 7 15 13
i-1 i
sorted unsorted
17
7/03/2025
While doing the proof, we need to show that if the LI is true at iteration k, it is true
at iteration k+1
2 4 5 8 10 12 7 15 13
i-1 i
sorted unsorted
The invariant currently holds
While doing the proof, we need to show that if the LI is true at iteration k, it is true
at iteration k+1
j
2 4 5 8 10 12 7 15 13
i-1 i
sorted unsorted
The invariant currently holds
18
7/03/2025
While doing the proof, we need to show that if the LI is true at iteration k, it is true
at iteration k+1
j
2 4 5 8 7 12 10 15 13
i-1 i
sorted unsorted
The invariant currently holds
While doing the proof, we need to show that if the LI is true at iteration k, it is true
at iteration k+1
j
2 4 5 8 7 12 10 15 13
i-1 i
sorted unsorted
The invariant should still be true… but it is not
19
7/03/2025
20
7/03/2025
Outline
What is the lower bound complexity of finding the minimum element in an array
of N elements?
o Ans: Ω(N), i.e. Omega(N): We (at least) need to look at every element.
Since the finding minimum algorithm we saw has O(N) worst-case complexity, it
is the best possible algorithm (i.e., optimal) in terms of time complexity.
Generally, notation O(N) (Big O) is used for Upper bound (indicating worst case
or at most this time). Ω(N) (Big Omega) is used for Lower bound (best case or at
least this time).
21
7/03/2025
Outline
22
7/03/2025
Intuition
Suppose you are given an array containing a permutation of the
numbers 1…N to sort.
Counting Sort
For example, consider the problem of sorting positive integers.
Determine the maximum value in the input.
Create an array “count” of that size.
count 1 0
2 0
3 0
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 0
8 0
23
7/03/2025
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 0
2 0
3 0
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 0
8 0
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 0
2 0
3 1
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 0
8 0
24
7/03/2025
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 1
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 0
8 0
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 2
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 0
8 0
25
7/03/2025
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 2
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 0
6 0
7 1
8 0
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 2
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 1
8 0
26
7/03/2025
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 1
8 0
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 0
27
7/03/2025
Counting Sort
Iterate through the input and count the number of times each value
occurs.
Do this by incrementing the corresponding position in “count”.
If we see a 3, increment 𝑐𝑜𝑢𝑛𝑡[3].
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output
28
7/03/2025
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1
29
7/03/2025
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3
30
7/03/2025
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3 5
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3 5
31
7/03/2025
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3 5 7 7
Counting Sort
Create “output”.
For each position in count:
o Append 𝑐𝑜𝑢𝑛𝑡[𝑖] copies of the value 𝑖 to output.
o So if 𝑐𝑜𝑢𝑛𝑡[7] = 2, then append 2 copies of 7.
count 1 1
2 0
3 3
4 0
Input (3,a) (1,p) (3,c) (7,f) (5,g) (3,b) (7,d) (8,w) 5 1
6 0
7 2
8 1
Output 1 3 3 3 5 7 7 8
32
7/03/2025
Output = empty
For x=1 to len(count):
o NumOfOccurrences = count[x]
o Append x to Output NumOfOccurrences times
Let N be the size of Input array and U be the domain size (e.g., max), i.e., U is the size of
count array.
Time Complexity:
O(N+U) – worst-case, best-case, average-case all are the same.
Space Complexity:
O(N+U)
Output = empty
For x=1 to len(count):
o NumOfOccurrences = count[x]
o Append x to Output NumOfOccurrences times
No, because it counts the values but does not distinguishes between them. In fact, it loses
any data associated with the values, so it is much worse than unstable. Let’s fix this!
Output 1 3 3 3 5 7 7 8
33
7/03/2025
What information would you need to correctly place the data with key “5”
in the output?
Construct count:
• For each key in input,
count
• count[key] += 1 1 1
2 0
3 3
4 0
5 1
6 0
7 2
8 1
34
7/03/2025
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 0
Construct position:
3 3 3 0
• Initialise first position as a 1
4 0 4 0
5 1 5 0
6 0 6 0
7 2 7 0
8 1 8 0
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 0
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 0
5 1 5 0
6 0 6 0
7 2 7 0
8 1 8 0
35
7/03/2025
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 0
5 1 5 0
6 0 6 0
7 2 7 0
8 1 8 0
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 0
6 0 6 0
7 2 7 0
8 1 8 0
36
7/03/2025
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
6 0 6 0
7 2 7 0
8 1 8 0
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
6 0 6 6
7 2 7 0
8 1 8 0
37
7/03/2025
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
6 0 6 6
7 2 7 6
8 1 8 0
Construct count:
• For each key in input,
count position
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position:
3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
6 0 6 6
7 2 7 6
8 1 8 8
38
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position: 3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) pair 7 2 7 6
from input 8 1 8 8
• Increment position[key]
Output
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position: 3 3 3 2
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (3,a)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
39
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position: 3 3 3 3
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (3,a)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 1
2 0 2 2
Construct position: 3 3 3 3
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
40
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 3
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 3
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
41
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 6
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
42
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 5
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (5,g) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
43
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (5,g) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 4
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
44
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 5
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 5
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 7
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f) (7,d)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
45
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 5
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 8
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f) (7,d)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 5
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 8
pair from input 8 1 8 8
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f) (7,d) (8,w)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
46
7/03/2025
Construct count:
count position
• For each key in input,
• count[key] += 1 1 1 1 2
2 0 2 2
Construct position: 3 3 3 5
• Initialise first position as a 1
• position[i] = position[i-1] + count[i-1] 4 0 4 5
5 1 5 6
Construct output
6 0 6 6
• Go through input, looking at each (key, val)
• Set output[position[key]] to the (key, val) 7 2 7 8
pair from input 8 1 8 9
• Increment position[key]
Output (1,p) (3,a) (3,c) (3,b) (5,g) (7,f) (7,d) (8,w)
1 2 3 4 5 6 7 8
FIT2004: Lecture 2 - Analysis of Algorithms
Counting sort can also be very inefficient for certain kinds of input, any
guesses?
o Example: N = 2, U = 9223372036854775807
o Input: [0, 9223372036854775807]
47
7/03/2025
Outline
Radix Sort
Sort an array of numbers, assuming each number has k digits (why is this often reasonable?)
▪ Use stable sort to sort them on the k-th digit
▪ Use stable sort to sort them on the (k-1)-th digit
▪ …
▪ Use stable sort to sort them on 1st digit
48
7/03/2025
Radix Sort
What happens if we don’t use stable sort?
Radix Sort
What happens if we process left to right (or most significant
digit to least?)
49
7/03/2025
Assume we are using stable counting sort which has time and space
complexity 𝑂(𝑁 + 𝑈)
50
7/03/2025
As we increase 𝑏…
Number of count sorts goes down
Cost of each count sort goes up
What would be the value of M which makes the total cost O(N)?
If M is O(Nc), then total cost: O(logN Nc * (N)) = O(CN) = O(N)!!!
51
7/03/2025
Sorting Strings
How can we apply the count sort/radix sort idea to strings?
The radix is now 26 (or however many characters we are using, e.g.
256)
Radix Sort
Sort an array of words in alphabetical order assuming each word consists of M letters each
⚫ Use stable sort to sort them on the M-th column
⚫ Use stable sort to sort them on the (M-1)-th column
⚫…
⚫ Use stable sort to sort them on 1st column
GO A L HERB GOA L TAL L A I MS
TALL B I RD R I DE HERB ANT S
A I MS L I KE L I KE R I DE B I KE
ANTS B I KE B I KE L I KE B I RD
B I RD R I DE M I KE B I KE F I SH
F I SH M I KE TALL M I KE GO A L
L I KE K I NG K I NG K I NG Sort on HERV
Sort on Sort on Sort on 1st
B I KE 4th F I SH 3rd A I MS 2nd A I MS K I NG
column
column column column
K I NG GO A L HERB B I RD L I KE
R I DE TALL B I RD F I SH M I KE
HERB A I MS F I SH ANTS R I DE
M I KE ANTS ANTS GO A L TALL
FIT2004: Lec-2: Analysis of Algorithms 104
52
7/03/2025
Algorithm’s Heroes
Betty Holberton (March 7, 1917 – December 8, 2001)
Reading
Course Notes: Sections 1.1, 1.5, 3.1, 3.3 and 3.4
You can also check algorithms’ textbooks for contents related to this
lecture, e.g.:
CLRS: Section 2.1, Chapter 8
Rou: Section 5.6
53
7/03/2025
Concluding Remarks
Summary
Loop invariants can be used to prove correctness
Stable sorting, in-place algorithms
Non-comparison sorting
Coming Up Next
Quick Sort and its best/worst/average case analysis
How to improve worst-case complexity of Quick Sort to O(N log N)
54