## Saturday 21 February 2015

### all about algorithm for beginners

http://crunchify.com/category/json/

http://www.geeksforgeeks.org/fundamentals-of-algorithms/

Def of big oh This is where Big O analysis helps – it gives us some basis for measuring the efficiency of an algorithm. A more detailed explanation and definition of Big O analysis would be this: it measures the efficiency of an algorithm based on the time it takes for the algorithm to run as a function of the input size. Think of the input simply as what goes into a function – whether it be an array of numbers, a linked list, etc.  # Big O Notation

Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case scenario, and can be used to describe the execution time required or the space used (e.g. in memory or on disk) by an algorithm.
Anyone who’s read Programming Pearls or any other Computer Science books and doesn’t have a grounding in Mathematics will have hit a wall when they reached chapters that mention O(N log N) or other seemingly crazy syntax. Hopefully this article will help you gain an understanding of the basics of Big O and Logarithms.
As a programmer first and a mathematician second (or maybe third or fourth) I found the best way to understand Big O thoroughly was to produce some examples in code. So, below are some common orders of growth along with descriptions and examples where possible.

### O(1)

O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set.
```bool IsFirstElementNull(String[] strings)
{
if(strings == null)
{
return true;
}
return false;
}```

### O(N)

O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. The example below also demonstrates how Big O favours the worst-case performance scenario; a matching string could be found during any iteration of the `for` loop and the function would return early, but Big O notation will always assume the upper limit where the algorithm will perform the maximum number of iterations.
```bool ContainsValue(String[] strings, String value)
{
for(int i = 0; i < strings.Length; i++)
{
if(strings[i] == value)
{
return true;
}
}
return false;
}```

### O(N2)

O(N2) 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(N3), O(N4) etc.
```bool ContainsDuplicates(String[] strings)
{
for(int i = 0; i < strings.Length; i++)
{
for(int j = 0; j < strings.Length; j++)
{
if(i == j) // Don't compare with self
{
continue;
}

if(strings[i] == strings[j])
{
return true;
}
}
}
return false;
}```

### O(2N)

O(2N) denotes an algorithm whose growth will double with each additional element in the input data set. The execution time of an O(2N) function will quickly become very large.

### Logarithms

Logarithms are slightly trickier to explain so I’ll use a common example:
Binary search is a technique used to search sorted data sets. It works by selecting the middle element of the data set, essentially the median, and compares it against a target value. If the values match it will return success. If the target value is higher than the value of the probe element it will take the upper half of the data set and perform the same operation against it. Likewise, if the target value is lower than the value of the probe element it will perform the operation against the lower half. It will continue to halve the data set with each iteration until the value has been found or until it can no longer split the data set.
This type of algorithm is described as O(log N). The iterative halving of data sets described in the binary search example produces a growth curve that peaks at the beginning and slowly flattens out as the size of the data sets increase e.g. an input data set containing 10 items takes one second to complete, a data set containing 100 items takes two seconds, and a data set containing 1000 items will take three seconds. Doubling the size of the input data set has little effect on its growth as after a single iteration of the algorithm the data set will be halved and therefore on a par with an input data set half the size. This makes algorithms like binary search extremely efficient when dealing with large data sets.

http://en.wikipedia.org/wiki/PageRank PageRank is an algorithm used by Google Search to rank websites in their search engine results. PageRank was named after Larry Page, one of the founders of Google.
------------------------------------------------------------------------------------
http://bigocheatsheet.com/

# Know Thy Complexities!

Hi there!  This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science.  When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them.  Over the last few years, I've interviewed at several Silicon Valley startups, and also some bigger companies, like Yahoo, eBay, LinkedIn, and Google, and each time that I prepared for an interview, I thought to myself "Why oh why hasn't someone created a nice Big-O cheat sheet?".  So, to save all of you fine folks a ton of time, I went ahead and created one.  Enjoy!
 `Good` `Fair` `Poor`

## Searching

Algorithm Data Structure Time Complexity Space Complexity

Average Worst Worst
Depth First Search (DFS) Graph of |V| vertices and |E| edges `-` `O(|E| + |V|)` `O(|V|)`
Breadth First Search (BFS) Graph of |V| vertices and |E| edges `-` `O(|E| + |V|)` `O(|V|)`
Binary search Sorted array of n elements `O(log(n))` `O(log(n))` `O(1)`
Linear (Brute Force) Array `O(n)` `O(n)` `O(1)`
Shortest path by Dijkstra,
using a Min-heap as priority queue
Graph with |V| vertices and |E| edges `O((|V| + |E|) log |V|)` `O((|V| + |E|) log |V|)` `O(|V|)`
Shortest path by Dijkstra,
using an unsorted array as priority queue
Graph with |V| vertices and |E| edges `O(|V|^2)` `O(|V|^2)` `O(|V|)`
Shortest path by Bellman-Ford Graph with |V| vertices and |E| edges `O(|V||E|)` `O(|V||E|)` `O(|V|)`