1.2 Algorithms
How many indicies do elements in 2d have?
2
What is an algorithm?
A set of step-by-step instructions used to solve a problem or carry out a task.
What are the logical operators?
AND (ensures overall statement is only true if all individual statements are true), OR (evaluates if any of the individual statements are true), NOT (used to reverse the logical state of the other operators)
What is the operator precendence?
NOT, AND, OR
What are relational operators used for?
Relational operators are used to compare different items of data.
What are trace tables?
Trace tables are used to track the values of each variable and how they change while the programming is running/being executed.
What are records?
a collection of data objects of different data types under the same identifier
What is an array?
a collection of data objects of the same data type under the same identifier
What are the arithmetic operators?
addition, subtraction, division, multiplication, modulus (returns the remainder after division), integer division (returns an integer after division), exponentiation (powers)
State the best and worst case of the bubble sort algorithm.
best - list is already sorted (only one pass needed) worst - reverse sorted list (full pass is needed for every item _ the final pass)
How do the sorting algorithms work?
bubble - compares adjacent data items and orders them. it starts at the beginning of the list and compares the first 2 items. if they are not in the correct order they are swapped, otherwise, no change occurs. The next 2 items are compared, and swapped if they aren't in order - this continues until the end of the list is reached - at which point the first pass is complete. The largest number in the list will bubble to the end if it isn't there already. Perform more passes until no more swaps are made - as the list has been sorted into order. merge - the list is broken into its component parts by finding the midpoint of the list and splitting into two, to form smaller lists, each of which is broken, repeatedly until each list holds only a single item. The first item of each list is compared to the first item in the adjacent list until all items have been compared and reassembled in order and merged back into one list.
When to choose bubble or merge sort?
bubble: - for shorter lists merge: - long list
State advantages for each sort algorithm.
bubble: adv - simple to code adv - no extra storage is needed to make copies of data dis - longer lists take a lot longer to sort merge: adv - longer lists don't extend execution time by too much dis - additional memory is needed for copies of lists dis - complex and usually involves recursion
What are errors?
bugs in programs that prevent them from executing or causing them to produce inaccurate/unexpected results.
What are the 2 kinds of repetion and in which scenario is each one used?
condition-controlled repetion - for when the number of times a loop is executed isn't known before the loop is started. count-controlled repetion - for when the number of times a loop is executed is known before the loop is started.
What are different data items called in arrays?
elements
What are the relational operators?
equal to (==), less than (<), greater than (>), not equal to (!=), less than (<=) or equal to, greater than or equal to (>=)
What are different data items called in records?
fields
How are two-dimensional arrays formed?
in two-dimensional arrays there is an array at each index position of a one-dimensional array.
State the purpose and symbol of assignment.
is the association of a piece of data with a variable or constant =
What is repetion?
it is the process of repeating a set of instructions until there is a desired outcome.
What is the purpose of selection?
it is used to choose between two or more options.
What are dry runs?
it's a mental or pencil-and-paper run-through of an algorithm, doing each step one at a time, helping the reader understand the algorithm or find errors, to investigate the functioning of an algorithm
What is the length of an array?
its the number of elements that the array contains.
What is iteration?
its the process of repeating a set of instructions for each item in a data structure
How do the seach algorithms work?
linear - the algorithm starts at the beginning of the list and moves through item by item until it finds the matching item and or reaches the end of the list. binary - compares the search item with the median item in a list and if a match isn't found, half the list including the median is discarded, split the list in half and recalculate the median, repeating until the search item is found or until there are no more items left to search.
What are the two standard search algorithms?
linear and binary
When to choose linear or binary search?
linear: - for unsorted or short lists binary: - for long sorted lists
State advantages and disadvantages for each search algorithm.
linear: adv - simple due to brute force method. dis - tries out every possibility until a solution is found or all possibilities are exhausted binary: adv - fast execution due to the divide and conquer method. dis - initial list must be sorted dis - complex and use recursion.
State the best and worst case of each search algorithm.
linear: best - seach item matches first item in the list. worst - seach item isn't on the list / last item binary: best - seach item matches the first median value of the list. worst - seach item isn't on the list / found at the last possible median position
What are the two standard sorting algorithms?
merge and bubble
What kind of data structure are strings?
one-dimensional
State the four programming constructs.
sequence, selection, repetition and iteration
What are data structures?
specialised ways of storing data.
State 3 types of errors and how they can occur in programs.
syntax - occur when the rules of the programming language aren't followed. logic - occur when there is a flaw in the design of a program, that doesn't prevent it from running but causes it to produce an inaccurate/unexpected result. runtime - occur during program execution when the processor is asked to perform an impossible operation e.g. open a non-existent file or divide by 0, causing the program to crash.
State the name of, draw and describe the function of the 6 flowchart symbols. [18]
terminal - Denotes the start and end of an algorithm. decision/selection - Denotes a decision where there are 2 possible outcomes to be made. process - Denotes a process to be carried out. input/output - Denotes an input or output. subprogram - Denotes a pre-defined subprogram (a function or procedure that has its own flowchart). line - Shows the logical flow of the program.
What are identifiers?
they are names of variable and constants describing the data being stored.
What are indexes?
they are the particular location of data in a list
What are variables and constants and what are they used for?
they represent names locations in memory (holding binary bit patterns that can be manipulated by programs) used to store values in algorithms and programs.
What are logical operators used for?
to combine statements/operands which can be evaluated as true or false.
What's the purpose of programming constructs?
to control the flow a program
What are arithmetic operators used for?
to perform calculations
What are the naming conventions for variables and constants. Provide examples.
variables - camel case. e.g. firstName constants - all in upper case e.g. VAT
Whats the differenece between variables and constants?
variables can be changed while the program is running and constants must not.
How do arrays increase the efficiency of a program?
without arrays, a separate variable/identifier would have to be used for each element