ECS20 Handout 6
January 18, 2011
1. An
algorithm
is a finite set of precise instructions for performing a computation or for solving a
problem.
2. Properties that algorithms generally share:
Input, Output, Definiteness, Correctness, Finiteness, Effectiveness, Generality.
3. Case study 1:
finding the maximum element in a finite sequence
a
=
{
a
(1)
,a
(2)
,...,a
(
n
)
}
of
integers.
procedure find_max(a,n)
imax = 1
max = a(1)
for i := 2 to n
if max < a(i) then
imax = i
max = a(i)
endif
endfor
return (imax,max) // imax is the index (location) of the largest element max
Complexity: The total number of comparisons =
n

1 = Θ(
n
)
4. Case study 2: locating an element
x
in a list of distinct element
{
a
1
,a
2
,...,a
n
}
of integers, or
determine that it is not in the list.
Pseudocode for linear sort algorithm:
procedure linear_search(a,x)
i := 1
while (i <= n and x /= a(i) )
i := i + 1
endwhile
if i <= n then
location := i
else
location := 0
endif
return location
// the subscript of the term that equals x,
// or is 0 if x is not found
Complexity: The (average) number of comparisons =
n
2
= Θ(
n
)
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
5. Case study 3: locating an element
This is the end of the preview.
Sign up
to
access the rest of the document.
 Winter '08
 Staff
 Insertion Sort, Big O notation, IMAX

Click to edit the document details