unit 6- algorithms Flashcards
A computer game is written in a high-level programming language.
(a) State why the computer needs to translate the code before it is executed
interpreter translates code into machine code, instruction by instruction - the CPU executes each instruction before the interpreter moves on to translate the next instruction. Interpreted code will show an error as soon as it hits a problem, so it is easier to debug than compiled code.
Describe the difference between how a compiler and an interpreter would translate the
code.
Interpreter translates just one statement of the program at a time into machine code. Compiler scans the entire program and translates the whole of it into machine code at once
what is a constant in programming
Data values that stay the same every time a program is executed are known as constants
what is a binary search ?
It works by repeatedly dividing in half the portion of the list that could contain the item, until you’ve narrowed down the possible locations to just one.
how does a linear search work
Starting at the beginning of the data set, each item of data is examined until a match is made. Once the item is found, the search ends.
what is a bubble sort ?
each item is compared with the one on the right and swapped if larger. at the end of the first pass the largest item is last.
an example: ( 15 , 9,7, 6 )
the first pass (9,7,6,15)
the second pass ( 7,6,9,15)
the third pass ( 6,7,9,15)
what is an insertion sort ?
the algorithm sorts one data at a time.
its similar to how you wold sort a deck of cards
here is a list 9,5,4,15
use and describe the method of an insertion sort
first you compare 9 and 5 9 is bigger so you swap them
list is now 5,9,4,15
you now compare 4 to the first two sets of data its the smallest so therefor goes first.
list is now 4,5,9,15
you then compare 15 but 15 is the highest so stays at the back
here is a list 9,5,4,15
use the bubble sort to sort the list
first pass (5,4,9,15) second pass (4,5,9,15)
What is the method of a merge sort ?
Divide the unsorted list in two
Continue to divide these lists into two until there is just one item in each list
Now merge each list back until there is only one list remaining – which will be the fully sorted list
What are the method of a merge sort ?
Pros and cons of linear search
Easier to understand than binary and works on any list
Can be inefficent when looking at longer lists
Pros and cons of binary search
More efficent with longer lists
Harder to understand and only works on sorted lists
Pros and cons of a merge sort ?
More efficent than bubble and instertion with longer lists
Uses more memory
Pros and cons of bubble
Easier to understand
Inefficent in longer litsts