




















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 algorithm analysis, focusing on big o notation. Algorithm analysis is a subfield of computer science that offers tools for determining the efficiency of different methods for solving a problem. The importance of comparing the efficiency of different algorithms, particularly in relation to the number of elements in an array (n). It also covers the concept of big o notation, which measures an algorithm's time requirement as a function of the problem size. Examples of common growth-rate functions and their differences as n grows larger.
Typology: Slides
1 / 28
This page cannot be seen from the preview
Don't miss anything!





















Docsity.com
^
Area of computer science ^
Provides tools for
determining efficiency
of different
methods of solving a problem^
E.g., the sorting problem - which sorting method is more efficient ^
Comparing the
efficiency
of different methods of solution.
^ Concerned with
significant
differences
^ E.g.:
^ n
n^ or proportional to
(^2) n?
^ Big difference: e.g., for
n^ =100 it results in 100-fold difference; for
n=
1000 it results in 1000-fold difference; for
n^ = …
Docsity.com
^
Docsity.com
^
Counting
an algorithm’s
operations
^
Example: calculating a sum of array elements^ int sum = item[0];int j = 1;while (j <
n
)
{
<- 1 assignment <- 1 assignment <-
n
comparisons
{
sum += item[j];++j; }
<-
n
-1 plus/assignments <-
n
-1 plus/assignments Total: 3n operations
^
Notice: Problem size
n
= number of elements in an array
This problem of size n requires solution with 3n operations
Docsity.com
Docsity.com
^ Differences
among the growth-rate functions
grow with
n
^ See the
differences
growing on the diagram on the previous page
^ The bigger n, the bigger differences -- that’s why algorithm efficiency is “
concern for large problems only”
Docsity.com
^ Algorithm A is order f(n) —denoted O(f(n))—
if there exist
constants k and n
such that A requires <= k*f(n) time units 0
to solve a problem of size n >= n
0
^ Examples
^ n
^ n
O(n
2 ): k=1/5, n
^ 5*n
^ O(n): k=5, n
Docsity.com
^
How about
n
2 -3n+10?
^
It is
O(n
2 ) if
there exist k and n
such that 0
kn
2 ≥ n
2 -3n+10 for all n ≥
n
0
^ We see (fig.) that:
3 n
2 ≥ n
2 -3n+10 for all n ≥
^ So
k=3, n
^ More k
pairs could be found, but finding just one is
More k
pairs could be found, but finding just one is
enough to prove that
(^2) n -3n+10 is O(n
2 )
Docsity.com
^
^
Worst
easier to calculate
^
Worst
easier to calculate
^
More common ^
Average-case analysis is
harder to compute
^
Yields a
more realistic
expected behavior
Docsity.com
Divides the array into twoparts: already sorted, and notyet sorted.
values [ 0 ]
On each pass, finds thesmallest of the unsortedelements, and swap it into itscorrect place,
thereby
increasing the number ofsorted elements by one.
Docsity.com
values [ 0 ]
Docsity.com
values [ 0 ]
To find the smallest in UNSORTED:indexMin =
1
comp. 1:
check if values[2] = 10 < values[indexMin] = 24 - yes => indexMin =
2
comp. 2:
check if values[3] = 36 < values[indexMin] = 10 - NO
comp. 3:
check if values[4] = 12 < values[indexMin] = 10 - NO
Thus indexMin =
2;^
swap values[1] = 24 with values[indexMin] = 10 – see next slide
Docsity.com
values [ 0 ]
U N S
S O R T E D
To find the smallest in UNSORTED:indexMin =
2
comp. 1:
check if values[3] = 36 < values[indexMin] = 24 - NO
comp. 2:
check if values[4] = 12 < values[indexMin] = 24 -
yes => indexMin =
4
Thus indexMin =
4;^
swap values[2] = 24 with values[indexMin] = 12 – see next slide
Docsity.com
values [ 0 ]
Docsity.com