The simple Big-O Notation Post
Lets see what he has to say …
I make no claim to be a “computer scientist” or a software “engineer”, those titles alone can spark some debate, I regard myself as a software developer and I generally don’t study the math and science behind everything I do. I generally learn what is relevant and useful to my day to day functioning and only rarely go deeper and dabble in the theory behind it. This is one of those occasions, so I decided to scour the internet and see what I could pick up. I hope to keep this simple, practical and to the point.
Big-O:
- Describes how the algorithm scales and performs, in terms of either the execution time required or the space used.
- Is relative representation of complexity. This allows you to reduce an algorithm to a variable which in turn allows you to easily compare it to another.
- Describes an upper limit on the growth of a function, in the other words the “worst case scenario”.
There is also Big-Omega notation which looks at the lower bound / “best case scenario” stating that the algorithm will take at least X amount of time and Big-Theta which is tight bound to both lower and upper / “average”.
Some quick observations in determining Big-O:
- A Sequence of statements, or things like conditional checks are constant: O(1)
- A loop of statements result in : O(n) n being the number of loop executions.
- Nested loops are multiplied together: O(n2) where n is the times the outer loop executes and m is the times the inner loop executes.
Comparing the common notation examples:
(Thanks to Algorithms: Big-Oh Notation.)
n | Constant O(1) | Logarithmic | Linear O(n) | Linear Logarithmic O(n log n) | Quadractic O(n2) | Cubic O(n3) |
---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 1 | 1 |
2 | 1 | 1 | 2 | 2 | 4 | 8 |
4 | 1 | 2 | 4 | 8 | 16 | 64 |
8 | 1 | 3 | 8 | 24 | 64 | 512 |
16 | 1 | 4 | 16 | 64 | 256 | 4,096 |
1,024 | 1 | 10 | 1,024 | 10,240 | 1,048,576 | 1,073,741,824 |
1,048,576 | 1 | 20 | 1,048,576 | 20,971,520 | 1012 | 1016 |
Java code example:
Show examples of notations in the table above.
/** * The Class BigOExamples. */ public class BigOExamples { /** * Constant. O(1) * * @param n the n * @return the int */ public int constant(int n) { if (n > 1) { return n; } else { return 0; } } /** * Linear. O(n) * * @param n the n * @return the int */ public int linear(int n) { int sum = 0; for (int j = 0; j < n; j++) { sum += j; } return sum; } /** * Quadratic. O(n^2) * * @param n the n * @return the int */ public int quadratic(int n) { int sum = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { sum += j * k; } } return sum; } /** * Cubic. O(n^3) * * @param n the n * @return the int */ public int cubic(int n) { int sum = 0; for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { for (int l = 0; l < n; l++) { sum += j * k / (l + 1); } } } return sum; } /** * Logarithmic. O(log n). Binary Search. * * @param data the to search * @param key the key * @return the int */ public int logarithmic(Integer[] data, int key) { int startIndex = 0; int endIndex = data.length - 1; while (startIndex < endIndex) { int midIndex = (endIndex - startIndex / 2) + startIndex; int midValue = data[midIndex]; if (key > midValue) { startIndex = midIndex++; } else if (key < midValue) { endIndex = midIndex - 1; } else { return midIndex; } } return -1; } /** * Linear Logarithmic. O(n log n). Quick Sort. * * @param data the to search * @param key the key * @return the int */ public Integer linearLogarithmic(Integer[] data) { QuickSort<Integer> sorter = new QuickSort<Integer>(); sorter.sort(data); return data[0]; } }
/** * The Class QuickSort. * * @param <t> the generic type */ public class QuickSort<t extends Comparable<T>> { /** * Sort. * * @param array the array */ public void sort(T[] array) { array = quicksort(array, 0, array.length - 1); } /** * Quicksort. * * @param array the array * @param lo the lo * @param hi the hi * @return the t[] */ private T[] quicksort(T[] array, int lo, int hi) { if (hi > lo) { int partitionPivotIndex = (int) (Math.random() * (hi - lo) + lo); int newPivotIndex = partition(array, lo, hi, partitionPivotIndex); quicksort(array, lo, newPivotIndex - 1); quicksort(array, newPivotIndex + 1, hi); } return (T[]) array; } /** * Partition. * * @param array the array * @param lo the lo * @param hi the hi * @param pivotIndex the pivot index * @return the int */ private int partition(T[] array, int lo, int hi, int pivotIndex) { T pivotValue = array[pivotIndex]; swap(array, pivotIndex, hi); // send to the back int index = lo; for (int i = lo; i < hi; i++) { if ((array[i]).compareTo(pivotValue) <= 0) { swap(array, i, index); index++; } } swap(array, hi, index); return index; } /** * Swap. * * @param array the array * @param i the i * @param j the j */ private void swap(T[] array, int i, int j) { T temp = array[i]; array[i] = array[j]; array[j] = temp; } }
Common Data Structures and Relative functions:
Lists and Sets:
Structure | get | add | remove | contains |
---|---|---|---|---|
ArrayList | O(1) | O(1) | O(n) | O(n) |
LinkedList | O(n) | O(1) | O(1) | O(n) |
HashSet | O(1) | O(1) | O(1) | O(1) |
LinkedHashSet | O(1) | O(1) | O(1) | O(1) |
TreeSet | O(log n) | O(log n) | O(log n) | O(log n) |
Maps:
Structure | get | put | remove | containsKey |
---|---|---|---|---|
HashMap | O(1) | O(1) | O(1) | O(1) |
LinkedHashMap | O(1) | O(1) | O(1) | O(1) |
TreeMap | O(log n) | O(log n) | O(log n) | O(log n) |
Better is the enemy of good!
Byron
Related Articles:
a java program that 3 numbers from the keyboard and computes their mean and product