8. algorithms Flashcards
recursive subroutine
defined in terms of itself and calls within itself.
the process of executing this subroutine is called recursion
3 characteristics of recursive routine
- stopping condition or base case must be included which when met means that the routine will not call itself and will start to ‘unwind’
- for input values other than stopping condition, the routine must call itself
- the stopping condition must be reached after a finite number of calls
in-order transversal
transverse the left subtree
visit the root node (middle)
visit the right subtree
post-order family
transverse the left subtree
transverse the right subtree
visit the root (right)
pre-order traversal
visit the root (left)
transverse the left subtree
transverse the right subtree
big O notation in order of efficiency
constant
logarithmic
linear
polynomial
exponential
factorial
constant
o(1)
no loops or recursion - what to look out for
e.g. accessing an element in array
linear straight line graph - what the graph looks like
logarithmic
o(log n)
1/2 data sets
e.g. binary search
similar to linear graph but with a bump at the start
linear
o(n)
a single loop
e.g. linear search
‘directly proportional’
polynomial
o(n^2)
nested loops
e.g. bubble sort
half a u
exponential
o(2^n)
recursion
e.g. recursion
half a u closer to y axis
o n log n
e.g. merge sort
permutations
the permutation of a set of objects is the number of ways of arranging the objects.
e.g. if you have 3 objects A B C, you can choose any of A B C to be the first object, you then have two choices for the second object.
formula for calculating the number of permutations of 4 objects is…
4 x 3 x 2 x 1, written as 4!, spoke as “four factorial”
constant time
same amount of time to execute regardless of the size of the input data set
linear time
describes an algorithm whose performance will grow in linear time, in direct proportion to the size of the data set
polynomial time
describes an algorithm whose performance is directly proportional to the square of the size of the data set
exponential time
describes an algorithm where the time taken to execute will double with every additional item added to the data set.
the execution time grows in exponential time and quickly become very large.
logarithmic time
the time taken to execute an algorithm of order o(log n) (logarithmic time) will grow very slowly as the size of the data set increases.
factorial time
the time taken to execute an algorithm of order o(n!) will grow very quickly, faster than o(2^n).
time complexity of linear search
o(n)
time complexity of binary search
o(log n)
time complexity of merge sort
o(n log n)
depth-first (using a stack)
is implemented automatically during execution of a recursive routine to hold local variables, parameters and return addresses each time a subroutine is called. alternatively, a non-recursive routine could be written and the stack maintained as part of the routine.
start at root, follow one branch as far as it will go, then backtrack.