You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers how to refine and optimise algorithms as required by OCR J277 Section 2.4. Refining means improving an algorithm to make it more correct, clear, or robust. Optimising means making it more efficient — faster, using less memory, or both. These are important skills for producing high-quality, robust programs.
Refinement is the process of improving an algorithm or program. This might involve:
Refinement is iterative — you make improvements, test them, and then make further improvements.
Optimisation is the process of making an algorithm more efficient while maintaining the same correct output. Efficiency can be measured in terms of:
| Measure | Description |
|---|---|
| Time efficiency | How quickly the algorithm completes — fewer steps or comparisons |
| Space efficiency | How much memory the algorithm uses |
OCR Exam Tip: When asked to optimise an algorithm, focus on reducing unnecessary operations. Common optimisations include: removing redundant comparisons, using better algorithms (e.g. binary search instead of linear search), and eliminating unnecessary variables or loops.
Before refinement:
total = 0
count = 0
for i = 0 to 9
score = int(input("Enter score: "))
total = total + score
count = count + 1
next i
average = total / count
print("Average: " + str(average))
After refinement:
total = 0
for i = 0 to 9
score = int(input("Enter score: "))
total = total + score
next i
average = total / 10
print("Average: " + str(average))
What changed: The count variable is unnecessary because the loop always runs exactly 10 times. Removing it simplifies the code without changing the output.
Before optimisation — linear search that does not stop when the item is found:
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
target = input("Enter name: ")
found = false
for i = 0 to names.length - 1
if names[i] == target then
found = true
position = i
endif
next i
if found == true then
print("Found at index " + str(position))
else
print("Not found")
endif
After optimisation — stops searching as soon as the item is found:
names = ["Alice", "Bob", "Charlie", "Diana", "Eve"]
target = input("Enter name: ")
found = false
i = 0
while i < names.length AND found == false
if names[i] == target then
found = true
position = i
endif
i = i + 1
endwhile
if found == true then
print("Found at index " + str(position))
else
print("Not found")
endif
What changed: The for loop continues checking all elements even after finding the target. The while loop stops as soon as the item is found, saving unnecessary comparisons.
Before refinement — no input validation:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.