Algorithms Flashcards
ideal algorithm
will run quickly and take up as little space(memory) as possible.
Time complexity
How much time they need to solve a given problem
Space complexity
The amount of resources e.g. memory they require
Permutation
number of ways we can arrange a set of data - factorial e.g. 4! = 4x3x2x1
Constant
y = 3
Linear
y = 3x
Logarithmic (Big O + graph)
y = log(x)
O(logn)
Polynomial
y = x^2
Exponential graph
y = x^3
How to write Big O
Notation always assumes the worst case scenario,
1. remove all terms except the one with the largest factor
2. Remove any constants
e.g. 3n is expressed as O(n), this is a linear complexity that grows by ‘n’ number of times
Constant complexity
O(1)
Always executes in the same time regardless of the size of the data set - most efficient. Time is independent of input
e.g. Determining if a number is odd or even
Logarithmic complexity
O(log n)
Halves the data in each pass. Opposite to exponential and efficient with large data sets - efficient
e.g. Binary search
Linear Logarithmic -
Merge sort = O(n log n)
Linear complexity
O(n)
Performance is proportional to the size of the data set and declines as the data set grows. - medium efficiency
In the worst case scenario it must go through each item once
e.g. Linear search
Polynomial complexity
O(n^2)
Performance is proportional to the square of the size of the data set. - inefficient
A loop inside a loop
e.g. Bubble sort
Exponential complexity
O(2^n)
Doubles with each addition to the data set in each pass, opposite to logarithmic - very inefficient
Intractable
e.g. recursively calculating Fibonacci numbers
Constant Time
Time remains constant even when the number of output increases
Logarithmic Time
Time will grow as the size of the input increases
Linear Time
Time will be linear to the size of the input
Polynomial Time
Time will grow proportionally to the square of the size of the data set
Exponential Time
Time will be as the power of the number of inputs, grows very quick
Factorial Time O(n!)
Time will grow even quicker as data is input, very inefficient
Tractable problems
efficient, have a polynomial or less time solution e.g. O(1). O(n), O(log n), O(n^2)