Notes on algorithms, and examples

CSI30 Discrete Math I
March 23-25, 2015
'Algorithm' stems from the name of a Latin translation of a book written by Abū
‘Abdallāh Muḥammad ibn Mūsā al-Khwārizmī (c. 780 – c. 850), a Persian
mathematician, astronomer and geographer. Al-Khwarizmi wrote a book titled On the
Calculation with Hindu Numerals in about 825 AD, and was principally responsible for
spreading the Indian system of numeration throughout the Middle East and Europe. It
was translated into Latin as Algoritmi de numero Indorum (in English, "Al-Khwarizmi on
the Hindu Art of Reckoning"). The term "Algoritmi" in the title of the book led to the term
"algorithm". (From Wikipedia)
An algorithm is a finite set of precise instruction for solving a specific problem. It should
have these properties:
Input: An algorithm takes some value or set of values from a specific set.
Output: Based on the input values, an algorithm produces output values from some
specific set. The output values give the solution to the problem.
Definiteness: The steps of an algorithm must be defined precisely.
Correctness: The algorithm should produce the correct solution to the problem.
Finiteness: Given any valid input, the algorithm should produce the desired output
after a finite number of steps.
Effectiveness: It must be possible to perform each step of the algorithm exactly and in
a finite amount of time.
Generality: The algorithm should apply to all problems of the type, not just for a
particular set of input values.
Some important algorithms:
Finding the maximum element in a finite sequence
procedure maximum( a1, a2, a3, …, an: integers)
max:= a1
#Remember the largest seen so far.
for i := 2 to n
if max < ai then max := ai
#If the current item is larger than the largest
#seen so far, update the value of max.
return max, the largest element
Searching algorithms: Find the location of an element x, the target, in a list of elements,
or report that it is not present.
Linear search or sequential search
procedure linear search(x: integer, a1, a2, a3, …, an: distinct integers )
i := 1
while (i ≤ n and x ≠ ai)
#while there are still items to examine, and the current
#item is not x
i := I + 1
#go to the next index
if i ≤ n then location := I
#the while loop ends if either all items are examined
else location := 0
# or the item is found. Determine which one.
return location, the subscript of the term that equals x, or is 0 if x is not found.
Binary search
procedure binary search(x: integer, a1, a2, a3, …, an: integers in increasing order )
i := 1 ( i is left endpoint of the search interval)
j: = n ( j is the right endpoint of search interval)
while i < j
#the interval being examined has more than one
mid := floor((i + j)/2)
#find the middle of the interval
if x > amid then i = mid +1 #if x would be to the right of the middle, move the left
else j := mid
#endpoint in, else move the right endpoint in.
if x = ai then location := I
else location := 0
#either the element where the searched stopped is
#x, or it isn’t
return location, the subscript of the term that equals x, or is 0 if x is not found.
Sorting algorithms
Bubble sort successively compares adjacent elements, interchanging them if they are
in the wrong order.
Procedure bubblesort( a1, a2, a3, …, an: real numbers with n ≥2)
for i:=1 to n-1
for j := 1 to n-i
#Push the ith largest element to its position at the end
if aj > aj+1 then interchange aj and aj+1
return (a1, a2, a3, …, an) now in increasing order
Insertion sort creates a sorted initial sequence of the list, and inserts each element into
that sorted sequence in the correct position.
Procedure insertionsort( a1, a2, a3, …, an: real numbers with n ≥2)
for j:=2 to n
#j is the position of element being inserted
while aj > ai
#Find the correct position i for element aj
m:= aj
#Remember the element in position j
for k := 0 to j-i-1
#Move the elements back
aj-k := aj-k-1
ai := m
#Put the element that was in position j in the
#correct position
return (a1, a2, a3, …, an) now in increasing order
Some elements of the pseudocode:
Assignment statement:
variable :=expression
The value of expression is assigned to the variable.
Conditional constructions:
if condition then statement
If the proposition condition is true, then statement is carried out.
if condition then statement1
else statement2
If the proposition condition is true, then statement1 is carried out. If condition is
false, then statement2 is carried out.
Loop constructions
for variable:= initial value to final value
for variable:= initial value to final value
block of statements
while condition
while condition
block of statements