Standard algorithms
Diperkenalkan
9:16
Siklus Hidup Pengembangan Program
Setiap aplikasi di telepon Anda ditulis oleh seseorang seperti ini. Namun mereka tidak mulai dengan mengetik kode. Sebelum baris pertama, masalah dipelajari, solusinya dirumuskan…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
In this lesson
| English | Makna |
|---|---|
| linear search/ˈlɪnɪə sɜːtʃ/ | pencarian linear |
| bubble sort/ˈbʌbl sɔːt/ | pengurutan gelembung |
| counting/ˈkaʊntɪŋ/ | penghitungan |
| totalling/ˈtəʊtəlɪŋ/ | penjumlahan total |
The classic recipes
- A few standard algorithms appear again and again.
- You must know linear search 线性查找, bubble sort 冒泡排序, and the counting 计数/totalling 求和 patterns.
- Each is short, but worth recognising instantly.
Linear search
DECLARE Found : BOOLEAN
Found ← FALSE
FOR I ← 1 TO 10
IF List[I] = SearchValue
THEN
Found ← TRUE
ENDIF
NEXT I
OUTPUT Found
- A linear search checks each item in turn, from the start, until it finds the value (or reaches the end).
- It works on any list — no sorting needed.

Standard algorithms
compare adjacent, swap if needed
Step through a bubble sort — compare neighbours and swap until everything is in order.
A linear search finds a value by:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Linear search examines items one by one until it finds the value or reaches the end.
Bubble sort
FOR I ← 0 TO 8
IF List[I] > List[I + 1]
THEN
Temp ← List[I]
List[I] ← List[I + 1]
List[I + 1] ← Temp
ENDIF
NEXT I
- A bubble sort compares each side-by-side pair and swaps them if they are out of order.
- It repeats this until no more swaps are needed — leaving the list sorted.

A bubble sort puts a list in order by:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
It swaps out-of-order neighbours and repeats passes until no swaps are needed.
Totalling, counting, max/min/average
- Totalling — keep a running total:
Total ← Total + Value. - Counting — add 1 each time something happens:
Count ← Count + 1. - Maximum/minimum — keep the largest/smallest value seen so far.
- Average — divide the total by how many values there are.

Which line adds a value to a running total?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Total ← Total + Value accumulates a sum; Count ← Count + 1 counts occurrences.
Match each standard algorithm to how it works.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
These four — search, sort, total and max — are the building blocks of most exam algorithms.
Using totalling on the list [2, 4, 6, 8], what is the final total?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
2 + 4 + 6 + 8 = 20.
Worked example: one pass of bubble sort
Show the first pass of a bubble sort on 5 2 8 1, sorting into ascending order.
- Compare 5 and 2: swap →
2 5 8 1. Compare 5 and 8: no swap. Compare 8 and 1: swap →2 5 1 8. After one pass the largest value, 8, is in its final place. - More passes are needed: pass 2 gives
2 1 5 8, pass 3 gives1 2 5 8. A pass with no swaps shows the list is sorted, so the algorithm stops.
A linear search looks for 7 in the list 4 9 2 7 5. How many items does it compare before it finds 7, including 7 itself?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
It checks 4, 9, 2, then 7: four comparisons.
A program has totalled 5 marks to get 340. What is the average?
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Average = total ÷ count = 340 ÷ 5 = 68.
You've got it
- linear search checks each item in turn (works on any list)
- bubble sort swaps out-of-order neighbours, repeating until sorted
- totalling (
Total ← Total + Value) and counting (Count ← Count + 1) - max/min = keep the best so far; average = total ÷ count