Ten simple algorithm examples, from making tea to sorting a list, with clear steps and pseudocode for the computing ones.
Boil water. Put a tea bag in a cup. Pour in the water. Wait three minutes. Remove the bag. Add milk and sugar if you like.
Stop at the kerb. Look right, left, right again. If no vehicle is near, cross while still watching. Otherwise wait and repeat.
Wet hands. Apply soap. Rub palms, backs, fingers and thumbs. Rinse. Dry with a clean towel.
Assume the first number is the largest. If the second is bigger, it becomes the largest. If the third is bigger than that, it becomes the largest. Print the largest.
READ a, b, c largest = a IF b > largest THEN largest = b IF c > largest THEN largest = c PRINT largest
Divide the number by 2 and look at the remainder. If the remainder is 0 the number is even; otherwise it is odd.
READ n
IF n MOD 2 = 0 THEN
PRINT "even"
ELSE
PRINT "odd"
Start with a total of 0. Count from 1 to n, adding each number to the total.
READ n
total = 0
FOR i = 1 TO n
total = total + i
PRINT total
Look through a list one item at a time until you find what you want or reach the end.
READ list, target
FOR each item IN list
IF item = target THEN
PRINT "found"
STOP
PRINT "not found"
To guess a number from 1 to 100, always guess the middle of the range you have left. If the answer is 'higher', throw away the lower half; if 'lower', throw away the upper half. You will find it in at most 7 guesses.
Compare each pair of neighbours and swap them if the left one is bigger. After one pass over 5, 2, 4, 1 you get 2, 4, 1, 5, and the biggest number has moved to the end. Repeat passes until nothing swaps.
Maps use algorithms to find routes, video apps use them to suggest what to watch, and search engines use them to rank pages. They are far bigger than the examples above, but they follow the same rule: clear steps that turn input into output.
See how Ocoviz supports CBSE's computational thinking and AI direction.
Explore CBSE CT & AI