Applied Design, Skills, and Technologies · Design and Make: Grade 6 Designers, Makers and CodersComputational Thinking: Algorithms, Cyphers and Code
BC ADST 6: simple algorithms BC ADST 6: visual programmingAbout 45 minutes
Learning goals- I can describe simple algorithms that use sequence, selection and repetition, including searching and sorting.
- I can encode and decode a message with a simple cypher.
- I can show an algorithm as a flowchart or visual program, and name people from the history of computing.
Words to know
algorithm
A clear set of steps to solve a problem or complete a task.
sequence
Steps done in a set order.
selection
Choosing what to do based on a condition, like if-then-else.
repetition
Doing steps again, as in a loop.
cypher
A method for turning a message into a secret code (also spelled cipher).
What is an algorithm?
An algorithm is a clear set of steps to solve a problem. Computers follow algorithms exactly, so every step must be specific. Most algorithms are built from three parts:
- Sequence: steps in order. (Put on socks, then shoes.)
- Selection: a choice based on a condition. (If it is raining, take a jacket, else take a hat.)
- Repetition: repeating steps. (Repeat 10 times: dribble the ball.)
The curriculum also names algorithms for searching and sorting.
Searching: linear and binary
A linear search checks items one at a time, from the start. It works on any list but can be slow.
A binary search works on a sorted list. Check the middle item. If it isn't the one you want, throw away the half it can't be in, and repeat. Each guess cuts the list in half, so finding a number from 1 to 100 takes at most 7 guesses (because 2 x 2 x 2 x 2 x 2 x 2 x 2 = 128, which is more than 100).
A flowchart for binary search in a number-guessing game. The diamond is a selection; the loop is repetition.Sorting by swapping
One simple sorting algorithm compares neighbours and swaps them if they are in the wrong order. Repeat passes through the list until no swaps are needed.
Worked exampleSorting 7, 3, 9, 1 by swapping neighbours
Sort the list 7, 3, 9, 1 from smallest to largest by comparing neighbours and swapping when the left one is bigger.
- Pass 1: 7 and 3: swap, giving 3, 7, 9, 1. Then 7 and 9: no swap. Then 9 and 1: swap, giving 3, 7, 1, 9.
- Pass 2: 3 and 7: no swap. 7 and 1: swap, giving 3, 1, 7, 9. 7 and 9: no swap. Result 3, 1, 7, 9.
- Pass 3: 3 and 1: swap, giving 1, 3, 7, 9. 3 and 7: no swap. 7 and 9: no swap.
- Pass 4: no swaps are needed, so the list is sorted.
The sorted list is 1, 3, 7, 9. The algorithm uses repetition (passes), selection (swap only if the left is bigger) and sequence (compare in order).
Cyphers and code breaking
A Caesar cypher shifts every letter the same number of places along the alphabet. With a shift of 3, A becomes D, B becomes E, and so on. At the end, it wraps around: X becomes A, Y becomes B, Z becomes C. To decode, shift back by 3.
| Plain | A | B | C | D | E | H | I | L | R | U | X | Y | Z |
|---|
| Shift 3 | D | E | F | G | H | K | L | O | U | X | A | B | C |
Part of a Caesar cypher with a shift of 3. Example: CEDAR becomes FHGDU.when green flag clicked
set secret to (pick random 1 to 100)
set guesses to 0
repeat until <(answer) = (secret)>
ask [Guess my number] and wait
change guesses by 1
if <(answer) < (secret)> then
say [Higher!] for 1 seconds
else
if <(answer) > (secret)> then
say [Lower!] for 1 seconds
say (join [Got it in ] (guesses))
A Scratch-style visual program for a guessing game. It uses sequence, selection (if-else) and repetition (repeat until).
People who shaped computing
- Ada Lovelace (1840s) wrote notes on Charles Babbage's planned Analytical Engine, including a method for it to calculate numbers. Many call it the first published computer program.
- Herman Hollerith built machines that read punch cards to count the 1890 United States census much faster.
- Alan Turing helped break the German Enigma cypher during the Second World War and wrote early ideas about computers and thinking machines.
- Grace Hopper built one of the first compilers, which turns human-readable instructions into machine code, and helped make programming languages more like English.
Programming languages have changed from switches and punch cards to typed code to visual programming tools such as Scratch, where you snap blocks together.
Be specific
Computers do exactly what you say, not what you mean. "Draw a square" is vague. "Repeat 4 times: move 100 steps, turn right 90 degrees" is an algorithm.
Hands-on activityCode and crack
- Write a short message (5 to 10 letters) and encode it with a Caesar cypher. Choose your own shift.
- Give it to a partner with the shift number. Can they decode it?
- Challenge: give a partner a coded message with no shift number. How could they crack it? (Hint: there are only 25 shifts to try.)
- Play the guessing game with a partner using binary search. Count your guesses for a number from 1 to 100.
- If you have Scratch, build the guessing game from the code above.
PuzzleDecode this Caesar cypher message with a shift of 3: ELUFK
Shift each letter back 3: E to B, L to I, U to R, F to C, K to H. The answer is BIRCH, a tree that grows across much of B.C.
Let's practise
1Name the three building blocks of most algorithms.
Sequence, selection and repetition.
2"If the light sensor sees dark, turn on the lamp." Is that sequence, selection or repetition?
3Encode DOG with a Caesar shift of 3.
4Decode KHOOR with a Caesar shift of 3.
5What must be true about a list before you can use binary search?
6What is the most guesses binary search needs for a number from 1 to 100?
More practice
1Sort 5, 2, 8 by swapping neighbours. Show pass 1.
5 and 2: swap, giving 2, 5, 8. 5 and 8: no swap. The list 2, 5, 8 is sorted.
2Encode XYZ with a shift of 3.
ABC (the alphabet wraps around).
3Who helped break the Enigma cypher in the Second World War?
Alan Turing (with many others at Bletchley Park).
4What did Herman Hollerith's punch card machines help count?
The 1890 United States census.
5Write a repeat loop to draw a triangle with equal sides.
Possible answer: repeat 3 times: move 100 steps, turn right 120 degrees.
Check your understandingQuiz
1. Which part of an algorithm makes a choice?
2. With a Caesar shift of 3, what does CAT become?
3. Binary search only works on a list that is...
4. Who wrote notes on the Analytical Engine that many call the first published computer program?
For parents and teachersHow to help at home: Play a number-guessing game and see if your child uses binary search. Exchange Caesar cypher notes. Ask your child to write an algorithm for a daily task (making toast) and follow it exactly as written; find the missing steps together.
Common mistakes: Vague steps, forgetting to wrap around the alphabet (X, Y, Z), using binary search on an unsorted list, and stopping a sort before a full pass with no swaps.