Chapter 3 Algorithms Integers
Chapter 3 Algorithms Integers
ALGORITHMS. INTEGERS
OUR GOAL
• Algorithms
• Linear search Binary search (Group 1)
• Bubble Sort Insertion sort (Group 2)
• The growth of functions (VinhDP)
• Complexity (VinhDP)
• Integers
• Divisibility: mod, div, congruent (Group 3)
• Applications of mod: (Group 4)
• Integer representations (Group 5)
• Primes and composites, gcd and lcm, The
Euclidean algorithm (Group 6)
Algorithms
• An algorithm is a finite sequence of precise
instructions for performing a computation or
for solving a problem.
• Language: natural language, programming
language, pseudocode
• Example.
Properties
• Input.
• Output.
• Definiteness. // defined precisely
• Correctness. // correct output
• Finiteness. // finite number of steps for any input in
the set.
• Effectiveness. // in a finite amount of time.
• Generality // not just for a particular set of input values.
Algorithms
• Linear search O(n)
• Binary search O(logn)
• Bubble sort O(n2)
• Insertion sort O(n2)
The linear search algorithm
procedure linear search(x: integer, a 1, a2, ..., an:
distinct integers)
i := 1
while (i ≤ n and x axi ) a ?x a ?x a ?
1 2 3
i := i + 1 a1 a2 a3 … an
if i ≤ n then location := i
else location := 0
return location {location is the subscript of the term that
equals x, or is0 if x is not found}
How many comparisons (i ≤ n, x ai ) are required?
( Linear search )
Sorting algorithms
• Bubble sort
• Insertion sort
• More (in later chapters or in
exercises)
• Merge sort (chapter 4) // one
of the fastest
• Tournament sort (chapter 10)
Bubble sort
i=1 i=2
3a 2 2 2 2 a <a 2 2
1 >a2 1 2
2 3 3 3 3 3 1
a2<a3 a2>a3
4 4 4 1 1 1 3
a3 >a4 a3<a4
1 1 1 4 4 4 4
a4<a5
5 5 5 5 5 5 5
Bubble sort
i=3 i=4
2 1 1a
a1>a2 1 <a2
1 2 2
a2<a3
3 3 3
4 4 4
5 5 5
sorted
The bubble sort
procedure bubblesort(a1,...,an :
real numbers with n ≥ 2)
for i := 1 to n − 1
for j := 1 to n − i
if aj > aj+1 then swap(aj,
aj+1)
{output: a1,...,an is in
increasing order}
The Insertion sort
procedure insertion sort(a1,a2,...,an: real numbers with n
≥ 2)
for j := 2 to n // begin with the 2nd element
i := 1
while aj > ai // find the first element with incorrect position
i := i + 1 3 2j = 2 4 1 5
m := aj
for k := 0 to j − i − 1
2 3 j4= 3 1 5
aj−k := aj−k−1 2 3 4 1
j=4
5
i=1 m=1
ai := m // insert aj into ai
2
{a1, ... ,an is in increasing order} 2 3 4 5
It is usually not the most efficient. 1 2 3 4 5
1 2 3 4 5
The growth of functions
• Bubble sort: (n – 1).n/2
comparisons for sorting n
elements
• An other sorting algorithm: using
2nlog2n + 3n + 13 comparisons.
• Which one should be used?
The growth of important functions
1 < logn < n < nlogn < n2 < n3 < 2n < n!
How to estimate big-O of other
functions?
• How to estimate big-oh of functions such as
100n2 + 23nlog(n3) + 2017?
Using some rules in practice:
– 1. f(n) is O(g(n)) Cf(n) is O(g(n))
Ignore constants
– 2. If f1(n) = O(g1(n)) and f2(n) = O(g2(n)), then
o (f1 + f2)(n) = O(max(|g1(n)|,Ignore added
|g2(n)|).
smaller order
o (f1.f2)(n) = O(g1.g2(n)) terms
n+n+…+n
= n.n = n2 is O(n2)
O(n2) time complexity
Estimate big-oh
procedure printsth(n: positive integer)
begin
for i:=1 to n do
for k:=1 to n do
for j:=1 to n print do
print “I love you”
end
Estimate big-O of the given algorithm
n2 + n 2 + … + n 2
= n.n2 = n3 is O(n3)
O(n3) time complexity
Estimate big-oh
procedure printsth(n: positive integer)
begin
For i:=1 to n do
print “hi”
For j:=1 to n do
For k:=1 to i do
print “I love you”
end
Estimate big-O of the given algorithm
n + n2 is O(n2)
Divisibility
dividend 37 5 divisor
2 7
Quotient = 37 div 5
Remainder
37 mod 5
37 – 5 = 32 // count =1
32 – 5 = 27 // count =2
27 - 5 = 22
22 – 5 = 17
17 – 5 = 12
12 – 5 = 7
7–5=2 // count = 7
2 < 5 stop
-37 5
a b (mod m)
74037563479561712828046796097429573142
59318888923128084936232638972765034028
26627689199641962511784399584330502127
58537011896809828673317327310893090055
25051687706329907239638078671008609696
2537934650563796359
Review – chapter 3
Encrypt the message NEW using the function f
(p) = (p + 11) mod 26.