Data Structure Lect9 Week 3
Data Structure Lect9 Week 3
Lecture 9
Topics
Complexity of the Algorithm.
Euclidean’s Algorithm
Examples
Bezout’s Theorm
Hence, a linear search requires (n) comparisons in the worst case, because 2n + 2 is (n).
Average Case
The average number of operations used to solve the problem over all possible
inputs of a given size is found in this type of analysis.
Average case time complexity analysis is usually much more complicated than
worst-case analysis.
3 + 5 + 7 +···+ (2n + 1) = 2(1 + 2 + 3 +···+ n) + n
n n
Using the formula
1 + 2 + 3 +···+ n = n(n + 1)/ 2 .
Hence, the average number of comparisons used by the linear search algorithm
(when x is known to be in the list) is
2[n(n + 1)/2] + 1 = n + 2
n
Euclidean’s Algorithm
Computing the greatest common divisor of two integers directly from the prime
factorizations of these integers is inefficient.
The reason is that it is time-consuming to find prime factorizations.
We will give a more efficient method of finding the greatest common divisor,
called the Euclidean algorithm
The greatest common divisor of two integers a and b can be expressed in the
form sa + tb, where s and t are integers.
In other words, gcd(a, b) can be expressed as a linear combination with integer
coefficients of a and b.
For example,
gcd(6, 14) = 2,
and 2 = (−2) · 6 + 1 · 14.
GCD as a Linear Combination
An important result we will use throughout the remainder of this section is that
the greatest common divisor of two integers a and b can be expressed in the
form
sa + tb,
where s and t are integers.
In other words, gcd(a, b) can be expressed as a linear combination with integer
coefficients of a and b.