Sunday, July 17, 2011

Big-O Comparison

http://leepoint.net/notes-java/algorithms/big-oh/bigoh.html

Here is a table of typical cases, showing how many "operations" would be performed for various values of N. Logarithms to base 2 (as used here) are proportional to logarithms in other base, so this doesn't affect the big-oh formula.

constantlogarithmiclinearquadraticcubic
nO(1) O(log N) O(N) O(N log N) O(N2) O(N3)
1111111
2112248
412481664
81382464512
161416642564,096
1,0241101,02410,2401,048,5761,073,741,824
1,048,5761201,048,57620,971,52010121016


Searching

Here is a table of typical cases.

Type of SearchBig-OhComments
Linear search array/ArrayList/LinkedList O(N)
Binary search sorted array/ArrayList O(log N) Requires sorted data.
Search balanced treeO(log N)
Search hash table O(1)


Algorithmarray
ArrayList
LinkedList
access front O(1) O(1)
access back O(1) O(1)
access middle O(1) O(N)
insert at front O(N) O(1)
insert at back O(1) O(1)
insert in middleO(N) O(1)


Sorting arrays/ArrayLists

Some sorting algorithms show variability in their Big-Oh performance. It is therefore interesting to look at their best, worst, and average performance. For this description "average" is applied to uniformly distributed values. The distribution of real values for any given application may be important in selecting a particular algorithm.
Type of SortBestWorstAverageComments
BubbleSort O(N) O(N2)O(N2) Not a good sort, except with ideal data.
Selection sortO(N2)O(N2)O(N2) Perhaps best of O(N2) sorts
QuickSort O(N log N) O(N2)O(N log N) Good, but it worst case is O(N2)
HeapSort O(N log N)O(N log N)O(N log N) Typically slower than QuickSort, but worst case is much better.