unit 12 Flashcards
(69 cards)
what are the features of a good algorithm?
- has clear stated steps that produce correct output for valid input
- should allow for invalid inputs
- must always terminate at some point
- should execute efficiently in as few steps as possible
what determines if an algorithm is efficient?
how many calculations are being executed
number of calculations relative to how many inputs there are
not speed
not amount of lines
what is a function?
a set of rules that connects the input to the output
what is the form of a linear function?
f(n) = an + b
what is the order of magnitude for a linear function?
O(n)
what is the form of a quadratic function?
f(n) = an^2 +bn + c
what is the order of magnitude of a quadratic function?
O(n^2)
what term is compared in algorithms?
the dominant term
what is the form of a logarithmic function?
f(n) = alog2n
what is the order of magnitude of a logarithmic function?
O(log n)
what is the behaviour of a quadratic function?
as n becomes large the n^2 term increases much faster than other terms
what is the behaviour of a logarithmic function?
f(n) increases very slowly in relation to n
what is big o notation?
the measure of the time or space complexity of an algorithm
what is the largest time complexity?
O(n!)
what are the different types of sort?
merge, bubble, insertion, quick
how does a bubble sort work?
passes are made through the array with each item being compared with the adjacent item and swapped if necessary
what is the time complexity for a bubble sort?
O(n^2)
what is an insertion sort?
takes each element and inserts into correct position until all are sorted
what is the time complexity for an insertion sort?
O(n^2) on average
how does merge sort work?
-divide unsorted list into n sublists each containing one element
-repeatedly merge sublists to produce new sublists until only one sublist left
what is the average time complexity for merge sort?
O(n log n)
how does quick sort?
-chooses a pivot point which divides list into 2 sub lists
-has left and right pointers
-move left pointer until value is larger than pointer
-move right pointer until value is smaller than pointer
-swap pointer values
what is the time complexity of a quick sort?
O(n log n)
why is the time complexity of quick sort O(n log n)?
length n, pivot in middle of list so n divisions
each of n items needs to be checked against pivot values