This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 307
Create Article: Big-O Notation #1081
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7c3f1a1
Create Article: Big-O Notation
angregor 2091736
Update Algorithms-Big-O-Notation
angregor d35ebd6
Update Algorithms-Big-O-Notation
angregor f5deac0
Update Algorithms-Big-O-Notation
angregor 441d205
Update Algorithms-Big-O-Notation
angregor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Big-O Notation | ||
|
||
In mathematics, Big-O notation is a symbolism used to describe and compare the limiting behavior of a function. | ||
In short Big-O notation is used to describe the growth or decline of a function, usually with respect to another function. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - In short Big-O notation
+ In short, big-O notation |
||
For example we say that x = O(x^2) for all x > 1 or in other words, x^2 is an upper bound on x and therefore it grows faster. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to toss in a line describing what you mean by the |
||
The symbol of a claim like x = O(x^2) for all x > *n* can be substituted with x <= x^2 for all x > *n* where *n* is the minimum number that satisfies the claim, in this case 1. | ||
Effectively, we say that a function f(x) that is O(g(x)) grows slower than g(x) does. | ||
|
||
Comparitively, in computer science and software development we can use Big-O notation in order to describe the time complexity or efficiency of our algorithm. | ||
Specifically when using Big-O notation we are describing the efficiency of the algorithm with respect to an input: *n*, usually as *n* approaches infinity. | ||
When examining algorithms, we generally want a lower time complexity, and ideally a time complexity of O(1) which is constant time. | ||
Through the comparison and analysis of algorithms we are able to create more efficient applications. | ||
|
||
## Examples | ||
|
||
As an example, we can examine the time complexity of the [bubble sort](https://github.com/FreeCodeCamp/wiki/blob/master/Algorithms-Bubble-Sort.md#algorithm-bubble-sort) algorithm and express it using big-O notation. | ||
|
||
#### Bubble Sort: | ||
|
||
```c++ | ||
// Function to implement bubble sort | ||
void bubble_sort(int array[], int n) | ||
{ | ||
// Here n is the number of elements in array | ||
int temp; | ||
for(int i = 0; i < n-1; i++) | ||
{ | ||
// Last i elements are already in place | ||
for(int j = 0; j < n-i-1; j++) | ||
{ | ||
if (array[j] > array[j+1]) | ||
{ | ||
// swap elements at index j and j+1 | ||
temp = array[j]; | ||
array[j] = array[j+1]; | ||
array[j+1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Looking at this code, we can see that in the best case scenario where the array is already sorted, the program will only make *n* comparisons as no swaps will occur. | ||
Therefore we can say that the best case time complexity of bubble sort is O(*n*). | ||
|
||
Examining the worst case scenario where the array is in reverse order, the first iteration will make *n* comparisons while the next will have to make *n* - 1 comparisons and so on until only 1 comparison must be made. | ||
The big-O notation for this case is therefore *n* * [(*n* - 1) / 2] which = 0.5*n*^2 - 0.5*n* = O(*n*^2) as the *n*^2 term dominates the function which allows us to ignore the other term in the function. | ||
|
||
We can confirm this analysis using [this handy big-O cheat sheet](http://bigocheatsheet.com/) that features the big-O time complexity of many commonly used data structures and algorithms | ||
|
||
It is very apparent that while for small use cases this time complexity might be alright, at a large scale bubble sort is simply not a good solution for sorting. | ||
This is the power of big-O notation: it allows developers to easily see the potential bottlenecks of their application, and take steps to make these more scalable. | ||
|
||
For more information on why Big-O notation and algorithm analysis is important visit this [hike](https://www.freecodecamp.com/videos/big-o-notation-what-it-is-and-why-you-should-care)! |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great opening, but maybe you would add a line or two explaining what you mean by limiting behavior.