Paper 2 Flashcards
Searching Algorithms D
Linear Search (can be used on Unordered list)
- Check first value, if desired value: stop
- Otherwise check second value, keep going until all elements have been checked or value is found
- not as efficient used on smaller lists
- once list has been ordered binary search is much more efficient
Binary Search (Ordered List)
- Put list in order
- take the middle value, if desired value: stop
- otherwise if its larger than desired value take list from left of middle value, if it is smaller take list to right of middle value
- take middle value of that list and keep repeating till value is found or list is too small to be split
Trace Tables D
- tests program for logic errors
- simulates the steps of algorithm
- each stage is executed one at a time allowing for inputs, outputs, variables and processes to be checked for the correct value for each stage
Key Concepts D
Computational Thinking
- use of computers to solve problems
- Developing algorithms to solve problems
- using abstraction, decomposition and algorithmic thinking
Abstraction
- removing unnecessary elements from problem
- using symbols and variables to represent real world problems
Decomposition
- Breaking down a large problem into a smaller problem
- smaller problems are easier to solve
- each part can be solved and tested independently
- parts are combined to produce full problem
- several ways to do this and they are all right
Algrothmitic Thinking
- a logical way of getting from the problem to the solution. If the steps you take to solve a problem follow an algrothim (set of instructions) then they can be reused and adapted to solve similar problems in the future
Error Types D
Syntax Error
- Code has not been correctly typed (typo)
- does not follow the rules or grammar of the programming language
- compiler or interpreter doesn’t understand something
Logic Error
- Program runs but produces unexpected result
- might be because program runs steps in incorrect order or multiplies instead of dividing
Sorting Algorithms D
Bubble Sort
- look at first two elements in a list, if 1 > 2 swap them, otherwise do nothing
- if they are in the wrong order you swap them or else do nothing
- move to the next pair in the list, repeat step 2. Until at end of the list (one pass), the last item will be in the correct place so don’t include in second pass
- repeat the passes till you have made your way through the list without any changes (no swaps)
Pros
- Simple algorithm that can easily be implemented
- efficient way to check if a list is already in order, only one pass to check if its in order
- Doesn’t use much memory as sorting is done using original list
Cons
- inefficient way to sort list, multiple passes or lots of comparisons
- Due to being inefficient it does not cope very well with a very large list
Merge Sort
- keep splitting list in half till individual elements are left
- merge pairs of sub-lists, each time you merge sort the items into right order
- keep merging till all pairs are in order
Pros
- more efficient and quicker than bubble sort and insertion sort for large lists
- it has consistent running time regardless of how ordered the items in the original list are.
Cons
- slower than other algorithms for small lists
- even if list is already sorted it still goes through the whole splitting and merging process
- uses more memory than other algorithms in order to create separate lists
Insertion Sort (CGP)
- look at second item in list
- compare to all items before it and insert number into right place
- repeat step 2 for third, fourth, fifth etc. Items until last number in the list has been inserted into correct place
advantages
- intuitive way of sorting things and can be easily coded
- copes very well with small lists - for this reason an insertion/merge hybrid sort is often used to take advantage of the strengths of each algorithm.
- all sorting is done on the original list so, like bubble sort it doesn’t require additional memory
- very quick to add items to an already ordered list
- quick at checking if a list is already sorted
Disadvantages
- doesn’t cope well with large lists
- requires lots of comparisons
Exam Reference Language D
Exam Reference Language
- looks like pretend code
- more formal way to represent algorithm for exam
- more like programming language but does not compile
- easy for programmers to read
Pseudocode D
Pseudocode
- uses short English words and statements to describe an algorithm
- Flexible
- less precise than programming language
- a little more structured than English
Completing an Algorithm D
Completing an Algorithm
- read what the algorithm should do
- note down the steps that should take place
- read steps of algorithms you already have
- use notes to write code to fill gaps
Correcting an Algorithm D
Correcting an Algorithm
- Read what the program should do
- Note down the steps that should take place
- Read each step of the algorithm
- At each step compare what the algorithm does to your notes
- take action to correct algorithm where it differs from your notes
Key Terms D
Algorithmic thinking - identifying the steps involved in solving a problem
Algorithm – a series of steps to perform an action or solve a problem.
Flowchart – a diagram showing inputs, outputs and processes within an algorithm
Process – an action that takes place
Pseudocode – simplified language used to design algorithms
Exam Reference Language – a more formal way of writing algorithms used within the exam
Sequencing D
Sequencing
- breaking down complex takes into simple steps
- order of steps matters
- step by step progress through program
Benefits
- each line follows the next
- can create simple programs very quickly
- Easy to follow for small programs
Disadvantages
- Not very efficient
- Difficult to follow for large problems
- Hard to maintain
Data Types D
Integers - whole numbers
Reals - decimal numbers
Boolean - TRUE or FALSE
Strings - alphanumeric characters
Casting is used to covert data from one type to another
Sub Programs D
Sub Programs
- used to save time and simplify code
- allows same code to be used several times without having to write it out each time
- Procedures are sets of instructions stored under a single name (identifier)
- Functions are similar to procedures but always return a value to the main program
- Parameters are values passed into the sub program, these are referred to as arguments when calling the sub program
- Both procedures and functions accept parameters
Arrays D
Arrays
- an ordered collection of related data
- each element in an array has a unique index, starting with 0
- all elements must be the same type of data
- usually a fixed size
- 1D arrays, each element needs a single index number, similar to lists
- 2D arrays, each elements needs two index numbers, similar to tables
String Manipulation D
String.length String.upper String.lower String.substring(1, 2) String.left(3) String.right(2) String+string
Keywords D
Variable
- a box in which data is stored
- contents change as program runs
Assignment
- process of changing data stored in a variable
- copies value into memory location
- different values may be assigned to a variable at different times during execution of program
- each assignment over writes current value
Constants
- Data does not change as program runs
- used to reference known values such as pi
Inputs
- may come from a user, a file or elsewhere
- usually treated as text even if it contains numbers
Outputs
- The end results of a program
- may be displayed on screen, written to file or sent to device
Operators
- used to manipulate and compare data
Operators D
Arithmetic Operators
- addition (+)
- subtraction (-)
- Multiplication (*)
- division (/)
- Modulus (MOD) remainder from division
- (DIV) Integer division
- (^) Exponentiation, to the power of
Comparison Operators
- (==) Equal to
- (!=) Not Equal to
- () greater than
- (>=) greater than or equal to
Boolean Operators
- AND, two conditions must be met for the statement to be true
- OR, at least one condition must be met for the condition to be true
- NOT, inverts result
Selection D
Selection
- allows programs to make decisions
- uses conditions to change the flow of the program
- selection may be nested inside one another
- IF statements compare sequentially and so order is important
- SELECT case has less typing but is less flexible
Random Numbers D
random(5)
Iteration D
Iteration
- iterating through a set of steps several times
- also known as looping
Count Controlled Iteration
- Repeats the same code a set number of times
- uses variable to track how many times the program has been run
- this variable can be used in the loop
- at the end of each iteration the variable is checked to see if the program should run again
Condition Controlled Iteration
- uses condition to determine how many times it should be repeated
- while loops run while condition is met
Authentication D
Authentication
- a coding method to check that the user is who they say they are, and that the user is allowed to access the program
- asking for username and password
authentication factors
- Something you are, finger print or iris scan
- Something you know, password, pin or secret answer
- Something you have such as a swipe card or mobile phone app
- 2FA is where two different authentication types are required to access the program
Input Validation D
Input Validation
- Applies rules to inputs, inputs that do not follow the rules is rejected to prevent it from crashing program
- Wont catch all errors as users still make typos
- Verification requires the user to enter this info twice to reduce the risk of this
Range Check
- Input must be within a range
Length Check
- Input must not be too long or too short
Presence Check
- The input must be present, for example requiring a credit card number for online order
Format Check
- input must be in the correct format
- for example must have @ and a . for email
Type Check
- data must be a specific type
- requiring currency input to only be numbers
Look-up table
- Checks data against table of acceptable values
Naming Conventions D
Naming Conventions
- Use same rules for naming to make it easier to read
- easy to read and meaningful
- Makes code easier to understand
Indentation D
Indentation
- allows code within a particular function or procedure to be group together
- Multiple levels of indentation may be used
- often used in IF statements
- makes code easier to read and understand
- makes it easier to focus on particular parts of program when needed