Professional Documents
Culture Documents
3 Complexity of Algorithms
Linear Search Algorithm
Determine if 3 is in the following lists
A= { 1 4 8 -1 2 }
B= { 3 4 8 6 5 }
C= { 1 2 4 9 10 11 12 14 19 10 -11 0 }
D= { 1 3 2 4 }
For each list ( problem ), how much time will the
algorithm take to finish the problem ?
1.
2.
Example
{ 1 11 50 6 8 1}
After first pass
{-11 1 6 8 1 50}
After Second Pass
{-11 1 6 -1 8 50}
After Third Pass
{-11 1 -1 6 8 50}
After Fourth Pass
{-11 -1 1 6 8 50}
After Fifth Pass
{-11 -1 1 6 8 50}
Done
Example
{ 1 11 50 6 8 1} Use Insertion Sort (sort in increasing order}
After first pass
{-11 1 50 6 8 -1} ( 1 comparison)
After Second Pass
{-11 1 50 6 8 -1} ( 2 comparisons)
After Third Pass
{--11 1 6 50 8 -1} ( 3 comparisons)
After Fourth Pass
{- 11 1 6 8 50 -1} ( 4 comparisons)
After Fifth Pass
{-11 -1 1 6 8 50} ( 2 comparisons, worst-case 5 comparisons)
Done
Complexity Classes
Linear search algorithm has linear complexity ( n ).
Bubble sort has quadratic complexity ( n2 ).
Binary search algorithm has logarithmic complexity ( log n ).
The big-O and big- provide a language of describing the
(time) complexity of algorithms.
Complexity Classes
For problems with input of size n, the number of operations required by the
algorithms.
(1)
Constant Complexity
( log n)
Logarithmic Complexity
( n)
Linear Complexity
( n log n)
n log n Complexity
( nb )
Polynomial Complexity
( bn )
Exponential Complexity
( n! )
Factorial Complexity
Complexity Classes
How do we known if a given type of problems is too difficult
to solve with computers?
Of course, some problems simply cannot be solved.
A problem that is solvable using an algorithm with
polynomial worst-case complexity is called tractable.
algorithm will produce the solution to the problem for reasonably sized
input in a relative short time.