Algorithms Intro
Algorithms Intro
General Introduction
, i s
g th
• Instructions:
• Heat the pan
r i n es
o s
• Fry x until.. (a do/while loop)
B u
• If it’s too thick, add water (an if/then statement)
! e
a h
• Just like a computer program, there are:
n
N ry o
• types/objects (100g of minced beef)
• statements (including composite and conditional loops,
v e l e
if..then, switch..case)
p
e
• observables/inputs (too thick, browned, boiling…)
m
a
Airline seat pricing
Calculate price of economy seat
ticket
Yes No
P>0?
This is an example of a
control algorithm.
https://www.researchgate.net/publication/328698232_Cooperative-
Competitive_Paradigm_in_Traffic_Signal_Synchronization_Based_on_
Floating_Car_Data
More examples:
• Ed Thorp – Blackjack algorithm(1961)
• Was later banned from some Las Vegas
Casinos
• Took to wearing disguises to gain entry
• Caused casinos to increase the size of the
card deck and ban portable computers
• He went on to found a hedge fund that
made 20% every year for 20 years in a
row
• One of the pioneers of algorithmic trading
• (Maths can make you rich)
on
Bluetooth LE off
and
an algorithm to allow co-existence
Algorithms are in nature
Nature
provides
inspiration for
development
of new
algorithms
Algorithms are in everyday life
• Think about the self-checkout machine at
the supermarket
• Let’s see if we can reverse-engineer its
algorithm
• What does the algorithm need to observe
(events)?
• barcode
• weight
• What programming features are used in the
algorithm?
• Are there do/while loops and if/then conditional
statements?
• Are there calculations?
• Is external data fetched from a backend?
• What causes the machine to say “Unexpected
Item in the Bagging Area”?
IDLE Happy path
Self-checkout Wait for Scan item
machine exercise event Put in bagging area
Scan item
Even Put in bagging area
t Scan item
type Put in bagging area
…etc
Finish and pay
Unhappy
paths
Item removed
before payment
What they
might ask
you about at
interview
First look at Sorting
• The problem:
• [3,4,1,5,2].sort() should equal [1,2,3,4,5]
• As well as just sorting, many programming solutions
depend on the result of a sort function:
• find
• check for missing item (or duplicates)
• compare (sets)
• We’ll briefly look today at animations of a simple
algorithm and a more complex one:
• Insertion sort
• Merge sort
Insertion
Sort for (I = 1 to LENGTH-1) {
for (J = 0 to J-1) {
if (A[I] < A[J]) {
doRotate(J,I);
break;
}
}
}
21 79 7 19 77 99 36 43 13 69
2 4
7 19 21 79 36 43 77 99 13 69
Recipe:
3
divide array into pairs
sort each pair (using swap) -> sorted sub-array 7 19 21 36 43 77 79 99 13 69
while (number of sorted sub-arrays > 1)
for each pair of sub-arrays:
“sortmerge” the pair -> sorted sub-array 5
(sortmerge is happening in steps 2,3,4,5 on 7 13 19 21 36 43 69 77 79 99
the right)
Pseudocode: recursive function call
function mergeSort (start, end) { // start, end of sub-array
sortmerge(start->divider, divider->end);
// this is what is happening in the yellow boxes
}
Call structure: recursive function call
merge (0,
1 10)
1
4 1 1 6
5 merge (8,
merge (0, 8)
10)
7 1
2 8 3
4 5 1
3 6 1
1 9 2
0 1
merge (0, merge (2,
merge (4, 6) merge (6, 8)
2) 4)
Experiment
Appreciating the art of sorting algorithms
A look ahead to the next workshop
What does the input data look like?What output is required?
• nearly sorted already • just the first few
• finite vocabulary (true/false, • just the comparison key
days of week, currency code) • is it sorted again using
• it’s actually changing all the another key (need stable
time! sort)
In the function my_play(), you have to change the code so that it will
consistently score above +30.
https://github.com/wai-t/CompSci/blob/main/ComputerScience/Algorithms_Intro/rock_paper_sciss
ors.js
let robo_plays = []; // list of bot’s game history
function my_play(game_num)
{
// TODO
// At the moment your strategy is to play SCISSORS every time.
// Your challenge is to replace the statement below with a strategy that will
Main function will loop round NUM_GAMES times, calling robo_play() and my_play()
and decide who wins each game.
End of General
Introduction
Self-checkout
machine exercise IDLE
Wait for
event
Y
Fetch Eve
Update item’s scan weigh Weight
Price and
nt matche
bill type
s
Weight N
Update
Y expected
weight
Weight N Unexpecte
matche IDLE d item in
s Wait for bagging
item area
Place removal
item in
bagging
IDLE area
Wait for
item