

















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
Definitions and algorithms for various searching and sorting techniques, including linear search, binary search, bubble sort, insertion sort, and greedy algorithms. It also covers the growth of functions and complexity analysis, including big o, big omega, and big theta notations. Additionally, it discusses the integers and division, prime numbers, and the euclidean algorithm for finding the greatest common divisor.
Typology: Study notes
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















The Fundamentals
Definition 13.1 Algorithms
An algorithm is a definite procedure for solving a
Problem: Searching Algorithmsproblem using a finite number of steps.
Locate an element
x
in a list of distinct elements
a 1 , a
2 ,... , a
n
or determine that it is not in the list.
Binary Search Algorithm procedure binary
search(
x :integer,
a 1 , a
2 ,... , a
n
integers in in-
i := 1
j :=
n
i < j
m
:=
i (^) +
(^) j ) / 2 ⌋
middle element
if
x > a
m
then
i :=
m
else
j
:=
m
x
=
a i then
location
i
location
Problem:Sorting Algorithms
Put a list
a 1 , a
2 ,... , a
n
in the increasing order.
The Bubble Sort procedure bubblesort(
a 1 ,... , a
n )
i := 1 to
n
−
for
j := 1 to
n
−
i
if
a j
a
j
then interchange
a j
with
a j
Definition 2 3.2 The Growth of Functionswhen solving an optimization problem. Greedy algorithm makes the ”best” decision for the current stepGreedy Algorithms
Let
f
and
g
be two functions from the set of integer
(or real) numbers. We say that
f (^) ( x )
is
g ( x ))
(which we write
as
f (^) ( x ) =
g ( x ))
) if there are two constants
and
k
such that
f (^) ( x ) | ≤
C | g ( x ) | ,
whenever
x > k
Theorem 1
Let
f (^) ( x ) =
a n x n + a n − 1 x n − 1 +
a 0
where
a 0 ,... , a
n
are real numbers. Then
f (^) ( x ) =
O ( x n ).
Fact 2
f 1 ( x ) =
g ( x ))
and
f 2 ( x ) =
g ( x ))
.
Then
( f 1 + f 2
x ) =
g ( x ))
.
f 1 ( x ) =
O ( g 1 ( x
and
f 2 ( x ) =
O ( g 2 ( x ).
Then
f 1 f 2 )(
x ) =
O ( g 1 ( x ) g 2 ( x
3.3 Complexity
time complexity
space complexity
Time analysis:
Worst-case analysis.
Average-case analysis.
Definition 6Modular Arithmetic
Let
a
be an integer and
m
a positive integer.
We
denote by
a
mod
m
the remainder when
a
is divided by
m
.
Definition 7
If
a
and
b
are integers and
m
is a positive integer
then
a
is congruent to
b
modulo
m
if
m | ( a − b ).
Fact 4
Let
m
be a positive integer and let
a
and
b
be two inte-
gers.
a
≡
b
(mod
m
)
if and only if
a
mod
m
b
mod
m
.
a
≡
b
(mod
m
)
if and only if there is an integer
k
such that
a
=
b
mk
Theorem 6The Division Algorithm
Let
a
be an integer and let
d
be a positive integer.
Then there exist unique integers
q
and
r
such that
a
=
dq
r
and
r < d
Definition 9Greatest Common Divisor and Least Common Multiple
Let
a , b be two integers not both zero. The largest
integer
d
such that
d
| a
and
d
| b
is called the greatest common
divisor of
a
and
b
. It is denoted by
gcd
a, b
Definition 10
The least common multiple of the positive inte-
gers
a
and
b
is the smallest positive integer that is divisible by
both
a
and
b
. It is denoted by
lcm
a, b
Theorem 7
Let
a , b
be positive integers. Then
a (^) ·
b
=
gcd
a, b
lcm
a, b
Euclidean Algorithm3.6 Integers and Algorithms Goal:
Find
gcd
a, b
a
≥
b .
Method:
Let
r 0
=
a, r
1
=
b
. Then
r 0 = r 1 q 1 + r 2
where 0
< r
2
< r
1
r 1 = r 2 q 2 + r 3
where 0
< r
3
< r
2
r... n − 2 = r n 1 q n − 1 + r n
where 0
< r
n
< r
n − 1
r n − 1 = r n q n
procedure gcd(Euclidean Algorithm
a, b
: positive integers)
x
:=
a
y
:=
b
y
6 = 0)
r
:=
x
mod
y
x
:=
y
y
:=
r
gcd
a, b
x
Lemma 9
Let
a
=
bq
r
where
a, b, q
and
r
are integers.
Then
gcd
a, b
gcd
b, r