CS Paper 1
CS Paper 1
1
3.1.1
Algorithm – A sequence of steps that can be followed to complete a task
Decomposition – The breaking down of a problem into multiple sub-problems where each sub-problem
completes an identifiable task
Abstraction – The process of removing unnecessary from a problem
Function – A block of code that runs only when it is called
Assign (=) – Operator used to assign values to variables in python
Parameters – The variables inside the parenthesis in the function definition that are placeholders for actual
values that the function needs
Argument – A value that is passed to a function when it is called
3.1.2
Linear Search
Starts at the first value of the list
Compares this value to target value
If they are not the same then it moves on to the next value and compares to the target value
Does this until the end of the list is reached
If the target value is found in the list, True is returned along with the value of the index (position)
If the target value is not found in the list, False is returned
Advantages
Useful if list is short
Best case is that the first value is the target value
Worst case is that the target value is either the last value in the list or the value is not in the list at all
Binary Search
Requires list to be sorted in ascending order – increases runtime
Divides list length using integer division by 2 to find midpoint
Compare midpoint to target value
If they are the same then the ‘True’ is returned along with the index of the target value
If the midpoint is larger than the target value, binary sort eliminates the values before the midpoint as
well as the midpoint and only focuses on the part of the list after the midpoint.
If the midpoint is smaller than the target value, binary sort eliminates the values after the midpoint as
well as the midpoint and only focuses on the part of the list before the midpoint
It then uses integer division to find the midpoint of the list and repeats the process
Once the highest index is greater than the lowest index and no number has been returned, the target
value isn’t present, then it returns False.
Differences
Bubble Sort
Starts at first value of the list
Compares to adjacent and swaps if the first value is larger than the adjacent value
If the numbers have swapped it continues to compare the number that has been swapped to the
adjacent values
If the number isn’t greater then it moves onto to the next value in the list
Once you reach the end of the list and swaps have been performed
Return to the start of the list and iterate through the list again using the same method
Once you reach the last value of the list and have not performed any swaps, then the list is sorted
Merge Sort
Utilises recursion
Divides the list into multiple sub-lists until each list contains one separate value.
Then these lists are compared and merged back into one sorted list
When the list has one value, it compares the first value of each list with the first value of the adjacent
list, the value that is smaller is add to the list above that was used to divide it into the separate lists
This is done until all of the separate lists are merged into one sorted list
Comparison
3.2
Iteration
Repeat-Until Loops
Controlled by condition at the end
Loop continues until the condition is true (while it is false)
The code in the loop is always run at least once
Infinite loop if condition is always false
While Loops
Controlled by condition at the start
Loop continues until the condition is false (while it is true)
If the condition is already false, the code is never run
Infinite if condition is always true
Do-While
Controlled by condition at the end
Loop continues until the condition is false (while it is true)
Code inside the loop is always run at least once
Infinite if condition is always true
Nested Loops
One loop inside another loop
For each time the outer loop repeats, the inner loop completes a full set of iterations
For Loop
Count-controlled loop
Runs a fixed number of times
Depends on the initial value of the loop variable
Sometimes has a step-count and increases by more than one
Data Structures
Arrays
Advantages of subroutines
Decreases complexity of code
Can be used again
Subroutines can be called multiple times and so Decreases repetition of code, therefore, easier to
read
Easier to test – can be tested outside the program – more effective testing
Easier to read and understand
Developed in isolation
Can be updated without affecting the overall program
Easier to discover errors
Syntax Errors – If the program doesn’t follow the rules of the coding language
Missing out brackets / missing colon after if statement
Runtime Errors – Occurs until the flow of control in the program reaches the line with problem
Eg. Using a variable before it is assigned to a value
Logic Errors – When the program doesn’t give the desired output / does not behave in the manner it was
designed to
No error messages
Using a variable in place of another variable
Trace tables good to find issues