Skip to content

mahmoudsaif/bigONotation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

bigONotation

Big O Notation Term Description

Big O notation Term 04/05/2018

Benefits of Knowing this term It helps you to analyze the scalability and efficiency of algorithms.

Best example to Understand that Term : Say you order Harry Potter Films Collection from Amazon and download the same film collection online at the same time. You want to test which method is faster. The delivery takes almost a day to arrive and the download completed about 30 minutes earlier. Great. What if I order several Blu-ray movies like The Lord of the Rings, Twilight, The Dark Knight Trilogy, etc. and download all the movies online at the same time? This time, the delivery still take a day to complete, but the online download takes 3 days to finish. For online shopping, the number of purchased item (input) doesn’t affect the delivery time. The output is constant. We call this O(1). For online downloading, the download time is directly proportional to the movie file sizes (input). We call this O(n). From the experiments, we know that online shopping scales better than online downloading.

How to touch that Term in our daily code: -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.

private bool IsFirstElementNull(IList elements) { return elements[0] == null; } -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.

private bool ContainsValue(IList elements, string value) { foreach (var element in elements) { if (element == value) return true; }

return false;

}

-O(N power 2) 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.

private bool ContainsDuplicates(IList 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;

} 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.

About

Big O Notation Term Description

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published