You are on page 1of 22

Worked Examples

Analysis
• The obvious measure of an input is the number
of elements in the array: i.e. n
• The most executed operations are in the “for
Loop”
• There are two operations in loop’s body:
• the comparison and
• the assignment

Question is, which of the two operations should we


Analysis
• Since the comparison is executed o each
repetition of the loop and assignment is not,
therefore comparison is the basic operation of
the this algorithm.

• Note: the number of comparisons will be the


same for all arrays of size n,

• What does this tell us about the worst, average


Analysis
• There is no need to distinguish between, best, average
and worst cases.
• It is enough to establish the number of times the basic
operation executes
• Let us denote : the number of times this comparison
is executed and try to find a formula expressing it as a
function of size ,
• The algorithm makes one comparison on each
execution of the loop, which is repeated for each
value of the loops variable within the bounds and
The summation
•Therefore, we get the following sum:

This sum is easy to compute because is


nothing else but a repeat of 1s times.
Computing C(n)
Example two
Analysis
•The obvious measure of an input is the
number of elements in the array: i.e. n
•The Inner most Loop contains only one
operation- Comparison is the basic
operation of the algorithm
Analysis
•The number of element comparison will
depend not only on n,
•But also on whether there are equal
elements in the array
•If there are, which array position they
occupy.
The Worst Case
•An array for which the number of
element comparison (n) is the largest
among all arrays of size n (Def)
•Inspection of inner loop suggest that
there two kinds of worst case inputs
The Worst Case
• Inputs for which the algorithm does not
exit the loop prematurely.
• Arrays with no equal elements
• Arrays in which the last two elements are
the only pair of equal elements.
• For such inputs, one comparison is made
for each repetition of the innermost Loop.
The Worst Case
• i.e. for each value of the loop’s variable
between its limits and
• and this is reapted for eack value of the
outer loop,
• i.e. For each value of the loop’s variable
between its limits 0 and
The Worst Case
• Accordingly, we get
•(n)=

•=

•=
The Worst Case

•= -

•=


Brute force algorithms –Example 1

The action of the algorithm is shown on the list 89,


45, 68, 90, 29, 34, 17 below:
Selection Sort
Selection Sort
• Straight forward analysis
• Input size is
• The basic operation of the algorithm is comparison

• The number of times it is executed depends only on


the array’s size
Selection Sort
• and is given by the following sum:

-1)-(i+1)+1]

=
Selection Sort
• The selection Sort is a algorithm on all inputs.
• Note However that the number of key swaps is only
• Or more precisely, n-1(one comparison for each
repetition of the loop)
Example 2 – Buble sort
Example 2 – Buble sort
• The action of the algorithm on the list, 89, 45, 68, 90, 29, 34, 17
Example 2 – Buble sort
Complete the analysis of this algorithm!

You might also like