Lesson 4 of 5
Algorithm Design
Create step-by-step instructions and foolproof recipes for computers to execute.
Learn it
An algorithm is simply a step-by-step list of instructions to get something done, just like a baking recipe or Lego instruction booklet.
Computers are super fast, but they are not very clever on their own. They do exactly what you tell them! If your algorithm misses a step, the computer will get confused or produce the wrong result.
Good algorithms are clear, precise, and have an unambiguous start and finish.
Key terms
- Algorithm
- A finite, unambiguous sequence of step-by-step instructions designed to perform a specific task.
- Sequencing
- The execution of algorithmic steps in a specific sequential order.
- Selection
- Using boolean conditions (if/else) to choose which execution path the algorithm follows.
- Iteration
- Repeating a block of instructions until a specific condition is satisfied.
Designing a Linear Search Algorithm
Follow the systematic steps needed to design an algorithm that searches for an item in an unsorted list.
- 1Step 1: Define Inputs and Target: Accept a list of items and the target value you want to locate.
- 2Step 2: Initialize Pointer: Set an index pointer to 0, representing the start of the list.
- 3Step 3: Inspect and Compare: Compare the item at the current index with the target value.
- 4Step 4: Branch on Result: If match is found, return the index position immediately.
- 5Step 5: Iterate or Terminate: If not found, advance index by 1 and repeat; if end of list is reached, return -1.
Linear Search Algorithm in Python
pythondef find_item_index(items, target):
# Step-by-step algorithm: iterate through every element
for index in range(len(items)):
if items[index] == target:
return index # Target found, exit early
return -1 # Target does not exist in the list
scores = [45, 88, 72, 95, 60]
print(find_item_index(scores, 95)) # Outputs: 3
print(find_item_index(scores, 100)) # Outputs: -1This algorithm uses sequencing (moving through elements), iteration (the for loop), and selection (if condition) to find a target value.
Try it
Complete the algorithm below to count how many positive numbers exist in a list:
def count_positives(numbers):
count = 0
for num in numbers:
if ______:
count += 1
return countChallenge
Design an algorithm in plain English or pseudo-code that finds the highest number in an unsorted list of integers.
Pick whichever way suits you — every mode earns the same bonus XP.
Write at least 40 more characters to submit.
Mark your own work
Guided walkthrough — 0/5 clues revealed
- Clue 1 locked — reveal it only if you get stuck.
- Clue 2 locked — reveal it only if you get stuck.
- Clue 3 locked — reveal it only if you get stuck.
- Clue 4 locked — reveal it only if you get stuck.
- Clue 5 locked — reveal it only if you get stuck.
Each clue costs 5 XP (never below 22 XP). You'd earn 43 XP right now.
Extension: Write your algorithm in Python and trace what happens when all numbers in the list are negative.