Algorithms
Algorithms
Cp 2023
Cp-engineers section ??
Moustapha Iradukunda
1
What is an algorithm?
An algorithm is “a finite set of precise
instructions for performing a computation or for
solving a problem”
A program is one type of algorithm
All programs are algorithms
Not all algorithms are programs!
Directions to somebody’s house is an algorithm
A recipe for cooking a cake is an algorithm
The steps to compute the cosine of 90° is an
algorithm
2
Some algorithms are harder than
others
Some algorithms are easy
Finding the largest (or smallest) value in a list
Finding a specific value in a list
Some algorithms are a bit harder
Sorting a list
Some algorithms are very hard
Finding the shortest path between Miami and Seattle
Some algorithms are essentially impossible
Factoring large composite numbers
4
Algorithm 1: Maximum element
Algorithm for finding the maximum
element in a list:
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8
i 1
90
8
7
6
5
4
3
2
6
Maximum element running time
How long does this take?
7
Properties of algorithms
Algorithms generally share a set of properties:
Input: what the algorithm takes in as input
Output: what the algorithm produces as output
Definiteness: the steps are defined precisely
Correctness: should produce the correct output
Finiteness: the steps required should be finite
Effectiveness: each step must be able to be
performed in a finite amount of time
Generality: the algorithm should be applicable to all
problems of a similar form
8
Searching algorithms
Given a list, find a specific element in the
list
9
Algorithm 2: Linear search
Given a list, find a specific element in the list
List does NOT have to be sorted!
i 1
8
7
6
5
4
3
2
11
Algorithm 2: Linear search, take 2
procedure linear_search (x: integer; a1, a2, …, an: integers)
i := 1
ii :=
:= 11
while ( i ≤ n and x ≠ ai )
while ( i ≤ n and x ≠ ai )
i := i + 1 x 11
if i i≤:=n ithen
+ 1 location := i
if
elsei ≤location
n then:=location
0 := i location 0
else location := 0
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
4 1 7 0 5 2 9 3 6 8
i 1
11
90
8
7
6
5
4
3
2
12
Linear search running time
How long does this take?
13
Algorithm 3: Binary search
Given a list, find a specific element in the list
List MUST be sorted!
Each time it iterates through, it cuts the list in half
i 1
6
7 m 5
8
7
6 j 10
8
7
15
Algorithm 3: Binary search, take 2
procedure binary_search (x: integer; a1, a2, …, an: increasing integers)
i := 1 while i < j if x = ai then location := iI
j := n begin else location := 0
m := (i+j)/2
if x > am then i := m+1 x 15
else j := m
end location 0
a1 a2 a3 a4 a5 a6 a7 a8 a9 a10
2 4 6 8 10 12 14 16 18 20
i 1
6
8 m 5
8
7 j 10
8
16
Algorithm 3: Binary search
A somewhat alternative view of what a
binary search does…
17
Binary search running time
How long does this take (worst case)?
18
Sorting algorithms
Given a list, put it into some order
Numerical, lexicographic, etc.
19
Algorithm 4: Bubble sort
One of the most simple sorting algorithms
Also one of the least efficient
It takes successive elements and “bubbles”
them up the list
22
Bubble sort running time
Bubble sort algorithm:
for i := 1 to n-1
for j := 1 to n-i
if aj > aj+1
then interchange aj and aj+1
23
Algorithm 5: Insertion sort
Another simple (and inefficient) algorithm
It starts with a list with one element, and inserts new elements into
their proper place in the sorted part of the list
Sorts
Bubble: n2 steps
Insertion: n2 steps
There are other, more efficient, sorting techniques
In principle, the fastest are heap sort, quick sort, and merge
sort
These each take take n * log2 n steps
In practice, quick sort is the fastest, followed by merge sort
27