weeks_1_2
weeks_1_2
Algorithms
Shahid Hussain
Weeks 1 & 2: Fall 2024
1
Introduction
Finding the maximum from a sequence
m ← xn
k ←n−1
Yes
k=0 Output m
No
Yes
xk > m m ← xk
No
k ←k−1
2
Finding the maximum from a sequence
Algorithm: findmax
Input: A sequence of numbers x1 , . . . , xn
Output: The maximum number m in the sequence
1. m = xk
2. k =n−1
3. while k > 0
4. if xk > m
5. m = xk
6. k =k−1
7. return m
Let m = 4 and x1 = 2.3, x2 = 7.1, x3 = 4.0, and x4 = 5.9. We
can trace the algorithm (flow chart) with this data. Following is
the trace 3
Finding the maximum from a sequence
m ← 5.9,
7.1
k ← 3, 2, 1, 0
• Is this algorithm correct?
• We employ invariant
• For the above algorithm we can place the invariant
m = max(xk+1 , . . . , xn ) before the first decision (the if
statement) and it will always remain true
4
Time Complexity
Time Complexity
5
Growth of Functions
30
log n
n
n log n
20 n2
n3
10
0 2 4 6 8
6
Oh and Other Notations
The Big-Omicron Notation
f (n) = O(g(n))
7
The Big-Omega Notation
f (n)
lim ̸= 0 implies f (n) = Ω(g(n))
n→∞ g(n)
8
The Big-Theta Notation
f (n)
lim = c implies f (n) = Θ(g(n))
n→∞ g(n)
9
The small-omicron Notation
f (n)
lim = 0 implies f (n) = o(g(n)).
n→∞ g(n)
10
The small-omega Notation
f (n)
lim = ∞ implies f (n) = ω(g(n)).
n→∞ g(n)
11
Examples
12
More Examples
Pn
1. Consider the series j=1 log j, clearly:
n
X n
X
log j ≤ log n = O(n log n)
j=1 j=1
Pn
2. We also prove that j=1 log j = Ω(n log n), therefore:
n
X
log j = Θ(n log n)
j=1
13
Problems: State True or False
1. n3 = O(n2 )
√
2. n log n = O(n n)
√
3. n2 (1 + n) = O(n2 log n)
√
4. log n + n = O(n)
√
5. n log n = O(n)
6. log n = O(1/n)
√ √
7. n + n = O( n log n)
14
Problems: Proof Questions
1. n2 − 3n − 18 = Ω(n)
2. 2n = O(n!)
3. n! = Ω(2n )
4. Does nlog n = O((log n)n )? Prove it
2
5. Prove or disprove: 2(1+O(1/n)) = 2 + O(1/n)
15
Searching
Linear Search
Algorithm: linearsearch
Input: A sequence of numbers a1 , . . . , an and a number x
Output: The index i such that ai = x, 0 otherwise
1. for j = 1 to n
2. if aj = x return j
3. return 0
16
Linear Search
17
Binary Search
18
Binary Search
Algorithm: binarysearch
Input: A sequence of numbers a1 , . . . , an and a number x
Output: The index i such that ai = x, 0 otherwise
Iterative Version Recursive Version
1. l = 1, r = n 1. m = ⌊(l + r)/2⌋
2. while l ≤ r 2. if am = x return m
3. m = ⌊(l + r)/2⌋ 3. else if am > x return
binarysearch(a1 , . . . , am−1 , x)
4. if am = x return m
4. else return
5. if am < x l = m + 1
binarysearch(am+1 , . . . , an , x)
6. else r = m − 1
7. return 0 19
Binar Search
20
Sorting
Sorting
21
Selection Sort
Algorithm: selectionsort
Input: A sequence of numbers A = ⟨a1 , . . . , an ⟩
Output: A permutation of A such that ai1 ≤ ai2 ≤ · · · ≤ ain
1. let B = ⟨⟩
2. for i = 1 to n
3. m = findmin(ai , . . . , an )
4. B = B ◦ ⟨am ⟩
5. am = ai
6. return B
Algorithm: insertionsort
Input: A sequence of numbers A = ⟨a1 , . . . , an ⟩
Output: A permutation of A such that ai1 ≤ ai2 ≤ · · · ≤ ain
1. for i = 2 to n
2. j=i
3. while j > 1 and aj < aj−1
4. aj , aj−1 = aj−1 , aj
5. j =j−1
6. return A
23
Insertion Sort with Binary Search
Algorithm: binary-insertionsort
Input: A sequence of numbers A = ⟨a1 , . . . , an ⟩
Output: A permutation of A such that ai1 ≤ ai2 ≤ · · · ≤ ain
1. for i = 2 to n
2. j = i, l = 1, r = j
3. while l ≤ r
4. m = ⌊(l + r)/2⌋
5. if aj < am then r = m − 1
6. else l = m + 1
7. aj , aj−1 = aj−1 , aj
8. return A
Algorithm: bubblesort
Input: A sequence of numbers A = ⟨a1 , . . . , an ⟩
Output: A permutation of A such that ai1 ≤ ai2 ≤ · · · ≤ ain
1. for i = 1 to n
2. for j = 1 to n − i
3. if aj > aj+1 then aj , aj+1 = aj+1 , aj
4. return A
25