Computer Science AS OCR
-
1-1-structure-and-function-of-the-processor as5 主题
-
1-2-types-of-processor as3 主题
-
1-3-input-output-and-storage as2 主题
-
2-1-systems-software as8 主题
-
2-3-software-development as5 主题
-
2-4-types-of-programming-language as4 主题
-
3-1-compression-encryption-and-hashing as3 主题
-
3-2-databases as3 主题
-
3-3-networks as8 主题
-
3-4-web-technologies as13 主题
-
html as
-
css as
-
css-styling as
-
javascript as
-
variables-and-constants-in-javascript as
-
outputs-in-javascript as
-
selection-in-javascript- as
-
for-loops-in-javascript- as
-
while-loops-in-javascript- as
-
strings-in-javascript- as
-
operators-in-javascript- as
-
nested-statements-in-javascript as
-
functions-and-procedures-in-javascript as
-
html as
-
4-1-data-types as8 主题
-
4-2-data-structures as4 主题
-
4-3-boolean-algebra as1 主题
-
5-1-computing-related-legislation as4 主题
-
5-2-moral-and-ethical-issues as9 主题
-
6-1-thinking-abstractly as3 主题
-
6-2-thinking-ahead as1 主题
-
6-3-thinking-procedurally as3 主题
-
6-4-thinking-logically as2 主题
-
6-5-thinking-concurrently as2 主题
-
7-1-programming-techniques as9 主题
-
8-1-standard-algorithms-and-big-o-notation as8 主题
suitability-of-algorithms as
Exam code:H046
Suitability of Algorithms
Why do we need to check the suitability of algorithms?
-
Algorithms must be compared to check the suitability for a specific set of data
-
Algorithms can be compared to determine how long they take to solve a particular problem and how much memory they require to solve problems
-
Generally, algorithms that finish quicker and use less memory are preferred
-
The measurement of time for an algorithm to complete is called “time complexity” whereas the measurement of memory is called “space complexity”
Time complexity
-
To measure time complexity, the execution time in terms of operations or steps performed is compared, not the numerical time taken in seconds and minutes
-
It is important to understand that an algorithm’s time complexity is independent of the hardware and CPU used to run it. This means a faster CPU with higher clock speed will still take the same number of steps or instructions to execute the algorithm compared to a slower CPU with lower clock speed
-
An analogy would be running a marathon. The marathon distance and course is the algorithm, whereas the runners are different CPUs with different speeds. The runners will complete the marathon with different times but the marathon is still the same distance
-
Alternatively, students completing several exam papers. Each exam has a different number of questions (like the number of instructions in an algorithm). Each student acts like a CPU. Some students will finish the exams faster than others. The number of questions are the same however for each student
-
-
The following algorithm calculates the sum of the first n integers:
function sumNumbers1(n) sum = 0 for i = 1 to n sum = sum + n next i return sum
endfunction
-
Compare this to the algorithm below which also calculates the sum of the first n integers
function sumNumbers2(n) sum = n * (n+1)/2 return sum
endfunction
-
Both algorithms above complete the same task, however the latter algorithm, sumNumbers2, is far more efficient in terms of the number of instructions executed
-
In sumNumbers1, the number of instructions executed are as follows:
-
+1 for (sum = 0)
-
+n for (for i = 1 to n)
-
-
The total number of instructions is therefore n+1. If n, the quantity of integers, is 10, then the total number of instructions executed is 11. If n is 1,000, then the total number is 1001
-
As n increases, the (sum = 0) line becomes more and more insignificant in contributing to the overall time complexity. As n increases, the algorithm becomes more inefficient
-
As n increases the time complexity of the algorithm becomes dependent on n
-
For further context, compare this to numerical time. If each instruction took one second to execute and if n is 1,000, then the algorithm would take 1,001 seconds to complete, where as if n is 10, it would take 11 seconds to complete
-
-
In sumNumbers2, the number of instructions executed are as follows:
-
+1 for (sum = n * (n+1)/2)
-
-
The total number of instructions is therefore 1. If n, the quantity of integers, is 10, then the total number of instructions executed is 1. If n is 1,000, then the total number is still 1
-
No matter how big n becomes, sumNumbers2 always takes the same amount of time. Its time complexity is constant
-
It is important to recognise that both algorithms share the same goal but complete them in very different ways
Space complexity
-
Space complexity is defined as how much memory an algorithm requires to complete
-
If additional space is required to duplicate the data for example, this will contribute to the overall space complexity
Responses