0% found this document useful (0 votes)
33 views4 pages

CS Paper 1

The document discusses algorithms, data structures, and programming concepts. It covers linear and binary search algorithms, bubble and merge sort, iteration, arrays, records, subroutines, and types of errors. Key differences between search algorithms and sorting methods are also outlined.

Uploaded by

Aarav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

CS Paper 1

The document discusses algorithms, data structures, and programming concepts. It covers linear and binary search algorithms, bubble and merge sort, iteration, arrays, records, subroutines, and types of errors. Key differences between search algorithms and sorting methods are also outlined.

Uploaded by

Aarav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

3.

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

 Linear search should be used if the list is short


 Binary search requires the list to be sorted and so has a longer run time – if list is sorted
 For longer lists, binary search should be used as it doesn’t have to go through all the values and
rather cuts down the list
 Best case scenario is that the target value is omega (1) for both and worst case scenario for linear
search is bigO(n) and for binary search is bigO(logn) therefore binary better for longer lists.

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

 Bubble sort better for small lists


 Bubble sort better for nearly sorted lists
 If list is completely randomized and is close to descending order, then merge sort is better
 Longer lists merge sort is better
 Merge sort requires more memory due to the creation of separate lists which all need allocated
memory slots
 Merge sort usually more efficient as we do not know the nature of the list

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

 Static data structure


 Fixed length
 All values must be same data type
 Arrays allocate memory in consecutive data slots
 Disadvantages – Can’t change size and this could mean too much or too less memory has been
allocated
Records

 Used to store a collection of data values


 Values can be of different data types
 Each item is called a field which is given a data type
 Fixed in length
Structured approach to Programming
Subroutines
 Block of code which completes a set of instructions
 When the instructions need to be carried out, the subroutine can be called in the main code
 Can be either functions or procedures – functions return a value, but procedures just complete the
set of instructions
Local Variable – Can only be used inside the structure they are declared in

 Not affected by anything outside the structure


 Can’t be used outside the structure
 Decreases complexity of code
Global Variable – Can be used any time after the declaration of the variable

 Used in the main body of program


 Determined using GLOBAL keyword before declaring the variable

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

Advantages of Structured Approach


 Decomposition allows programs to be broken down into separate sub-problems which completes an
identifiable task and can be implemented as sub-routines

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

You might also like