Big O Notation is just a way of
representing the general growth in
a computing difficulty of A task
as you increase the DATA set.
O(1)
The Big O notation for a constant function is 0(1). This describes an algorithm that will always execute in the same time(or space).
bool IsFirstElementNull(IList<String> elements){return elements[0] == null;}
O(N²)
With Quadratic time an algorithm run time is proportional to the square root of the amount of data.
O(N²) is the Big O notation for Quadratic time.
O(N²) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N³), O(N⁴) etc.
bool ContainsDuplicates(IList<string> elements)
{
for (var outer = 0; outer < elements.Count; outer++)
{
for (var inner = 0; inner < elements.Count; inner++)
{
// Don't compare with self
if (outer == inner) continue;
if (elements[outer] == elements[inner]) return true;
}
}
return false;
}Let's Compare linear search and binary search algirorithms:
linear search
looks down a list, one item at a time, without jumping. In complexity terms this is an O(n)search - the time taken to search the list gets bigger at the same rate as the list does.
binary search
is when you start with the middle of a sorted list, and see whether that's greater than or less than the value you're looking for, which determines whether the value is in the first or second half of the list. Jump to the half way through the sublist, and compare again etc. This is pretty much how humans typically look up a word in a dictionary (although we use better heuristics, obviously - if you're looking for "cat" you don't start off at "M"). In complexity terms this is an O(log n) search - the number of search operations grows more slowly than the list does, because you're halving the "search space" with each operation.
What is the Fibonacci Sequence?
A Fibonacci sequence is a group of numbers where you determine the next
number in the sequence by adding the previous two numbers in the sequence
together.
Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
The Fibonacci sequence can be described with the following formula:
Fn = Fn-1 + Fn-2 where the seed/original values are F1 = 1 and F2 =1.
function fib (n) { if (n < 2) { return [1]; } if (n < 3) { return [1, 1]; }
var a = fib(n - 1); a.push(a[n - 2] + a[n - 3]); return a;};
Comments
Post a Comment