Created
January 4, 2018 17:54
-
-
Save montyr75/f5bf1f613772fe5c5414f014adfb5a04 to your computer and use it in GitHub Desktop.
Big O notation, with Dart examples.
This file contains hidden or 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
// O(1) | |
// constant | |
bool isFirstElementNull(List<String> elements) { | |
return elements.first == null; | |
} | |
// O(n) | |
// growth is linear in direct proportion to the size of the data set | |
bool containsValue(List<String> elements, String value) { | |
for (String element in elements) { | |
if (element == value) return true; | |
} | |
return false; | |
} | |
// O(n^2), O(n^3), etc. | |
// proportional to the square (or greater exponent) of the size of the data set (nested loops) | |
bool containsDuplicates(List<String> elements) { | |
for (int outer = 0; outer < elements.length; outer++) { | |
for (int inner = 0; inner < elements.length; inner++) { | |
// Don't compare with self | |
if (outer == inner) continue; | |
if (elements[outer] == elements[inner]) return true; | |
} | |
} | |
return false; | |
} | |
// O(2^n) | |
// growth doubles with each addition to the data set | |
int fibonacci(int number) { | |
if (number <= 1) return number; | |
return fibonacci(number - 2) + fibonacci(number - 1); | |
} | |
// O(log n) | |
// growth curve that peaks at the beginning and slowly flattens out as the size of the data set increases |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment