Understand and apply standard search and sort algorithms (e.g. linear search, bubble sort)
Algorithm Design and Problem‑Solving 📚
1. What is an Algorithm?
An algorithm is a step‑by‑step recipe that tells a computer exactly how to solve a problem. Think of it as a cooking recipe: you need a list of ingredients (data), a set of instructions (steps), and a final dish (output). In computer science, the “ingredients” are usually numbers or words, and the “dish” is the result you want, like a sorted list or a found item.
2. Search Algorithms: Linear Search 🔍
Linear search is the simplest way to find an item in a list. Imagine you’re looking for your favourite book in a stack of unsorted books. You start at the top and check each book one by one until you find it or run out of books.
Algorithm Steps
- Start at the first element of the list.
- Compare the current element with the target value.
- If it matches, return the index (or the element itself).
- If it doesn’t match, move to the next element.
- Repeat until the end of the list is reached.
- If the target is not found, return “not found”.
Pseudocode
function linearSearch(list, target):
for i from 0 to length(list)-1:
if list[i] == target:
return i
return -1
Time Complexity
The worst‑case time is proportional to the number of elements, so we write it as $O(n)$.
3. Sort Algorithms: Bubble Sort 🔄
Bubble sort is a gentle way to arrange items, like arranging a line of books by height. Each pass “bubbles” the tallest book to the end, just like a bubble rises to the surface of water.
Algorithm Steps
- Start at the first element of the list.
- Compare the current element with the next one.
- If the current element is larger, swap them.
- Move to the next pair and repeat until the end of the list.
- After one full pass, the largest element is at the end.
- Repeat the whole process for the remaining unsorted part.
- Stop when no swaps are needed in a pass.
Pseudocode
function bubbleSort(list):
n = length(list)
for i from 0 to n-1:
swapped = false
for j from 0 to n-i-2:
if list[j] > list[j+1]:
swap(list[j], list[j+1])
swapped = true
if not swapped:
break
Illustration of Passes
| Pass | List After Pass |
|---|---|
| 1 | [3, 1, 4, 2, 5] |
| 2 | [1, 3, 2, 4, 5] |
| 3 | [1, 2, 3, 4, 5] |
Time Complexity
In the worst case, bubble sort performs about $n^2$ comparisons, so its time complexity is $O(n^2)$. It’s simple but not efficient for large lists.
4. Problem‑Solving Tips 🧩
- Understand the problem: Write down what you’re given and what you need to find.
- Plan a strategy: Choose an algorithm that fits the data size and required speed.
- Write pseudocode: Outline the steps before coding.
- Test with examples: Use small lists to check each step.
- Analyze complexity: Estimate how the algorithm scales with larger inputs.
5. Quick Recap 📌
- Linear search: $O(n)$ time, great for small or unsorted lists.
- Bubble sort: $O(n^2)$ time, easy to understand but slow for big data.
- Always choose the right tool for the problem size and constraints.
Revision
Log in to practice.