5 missions. Guess, learn, then check. Your place is saved.
The specification points and sources are listed under the lesson: see Sources.
Make a guess: Why does the Tube map ignore real distances, yet still work?
Beck's map is an abstraction. It removes detail a traveller does not need (true distances, tunnel shapes) and keeps what they do need (the lines, the order of stations and where to change), so planning a journey splits into simple sub-problems: which line, where to change, where to get off.
Make a guess: How can you find a bug without running the program?
| Symbol | Meaning |
|---|---|
| Rounded rectangle (terminator) | Start or Stop |
| Parallelogram | Input or output |
| Rectangle | Process, such as a calculation |
| Diamond | Decision: two exits, Yes/No or True/False |
| Rectangle with double sides | Subroutine (a separate sub-program) |
| Arrow (flow line) | The direction control flows |
count = 3
result = 1
while count > 0
result = result * 2
count = count - 1
endwhile
print(result)num = 37
steps = 0
while num > 1
num = num DIV 2
steps = steps + 1
endwhile
print(steps)mark = int(input("Enter mark"))
if mark > 50 then
print("Pass")
else
print("Fail")
endifA dry run follows the algorithm by hand, recording every variable and output in a trace table. Where a value or output differs from what it should be, you have found the logic error and its line, without running anything.
Make a guess: How can 20 checks find one name among a million?
found = False
i = 0
while i < 7 AND found == False // items holds 7 values: index 0 to 6
if items[i] == target then
found = True
else
i = i + 1
endif
endwhile
if found == True then
print(i)
else
print("Not found")
endiffirst and last mark the part of the list still being searched.first = 0
last = 14 // items holds 15 values: index 0 to 14
found = False
while first <= last AND found == False
mid = (first + last) DIV 2
if items[mid] == target then
found = True
elseif items[mid] < target then
first = mid + 1
else
last = mid - 1
endif
endwhile[3, 8, 11, 15, 19, 24, 27, 31, 36, 40, 44, 52, 58, 63, 70]
| Linear search | Binary search | |
|---|---|---|
| Data must be sorted? | No | Yes |
| Most checks for 15 items | 15 | 4 |
| Most checks for 1,000 items | 1,000 | 10 |
| Code | Simpler | More complex |
A binary search on the sorted names checks the middle one and discards half of the list each time. Twenty halvings are enough, because 2^20 = 1,048,576 is more than 1,000,000. A linear search might need all 1,000,000 checks.
Make a guess: Why is it called a bubble sort?
do
swapped = False
for i = 0 to 4
if items[i] > items[i + 1] then
temp = items[i]
items[i] = items[i + 1]
items[i + 1] = temp
swapped = True
endif
next i
until swapped == Falseitems[i] = items[i + 1]
items[i + 1] = items[i]In each pass, swaps carry the largest unsorted item along the list until it reaches its place at the end, like a bubble rising to the top of a drink. Each pass bubbles one more item into place, and a pass with no swaps shows the list is sorted.
Make a guess: Why does a fast sort start by chopping a list into pieces?
for i = 1 to 5 // items holds 6 values: index 0 to 5
current = items[i]
pos = i
while pos > 0 AND items[pos - 1] > current
items[pos] = items[pos - 1]
pos = pos - 1
endwhile
items[pos] = current
next i| Bubble sort | Merge sort | |
|---|---|---|
| Large lists | Slow: hundreds of thousands of comparisons for 1,000 items | Fast: under 9,000 comparisons for 1,000 items |
| Memory | Sorts in place, little extra memory | Needs extra memory for the split lists |
| Code | Simple | More complex |
| Nearly sorted list | Quick if only a few items are out of place | Still splits and merges everything |
Merge sort splits a list until every piece holds one item, and a single item is already sorted. Merging two sorted lists takes at most one comparison per item placed, so 1,000 items need under 9,000 comparisons, where a bubble sort can need hundreds of thousands.
You've worked through Algorithms for OCR GCSE Computer Science (J277). 🎉
Your stars: 0 / 0
Next: test yourself in the Verify stage.
→ Back to GCSE Computer Science · OCR
Specification points and sources for this lesson are listed at the bottom of the page.
The specification points and sources behind this lesson.