Top Topics in our Forum';
**Interactive equation of a circle. Click and drag to see equation in action!

A+    A−    B  
Algebra
Games
Geometry
Interactive
Trigonometry
Forum




AddThis Social Bookmark Button

Big Oh Notation

Best, Average and Worse Cases

The relationship between the number of 'operations' (n) and the Big Oh of the algorithm can easily be seen in the chart below.
 constantlogarithmiclinear quadraticcubic
nO(1) O(log N) O(N) O(N log N) O(N2) O(N3)
11 1 1 1 1 1
21 1 2 2 4 8
41 2 4 8 16 64
81 3 8 24 64 512
161 4 16 64 256 4,096
1,0241101,02410,2401,048,5761,073,741,824
1,048,5761201,048,57620,971,52010121016

Big Oh of Common Searching Algorithms

Search Type Big-OhComments
Linear search array/ArrayList/LinkedList O(N)  
Binary search sorted array/ArrayList O(log N) data must already be sorted.
Search balanced treeO(log N)  
Search hash table O(1)  
Other common
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)

Big Oh of Common Sorting Algorithms

By best, worst and average case for well known sorting algorithms. Animated Demonstration of Sorting Algorithms
Answer
Type of SortBestWorstAverage
BubbleSort
Insertion sort
Selection sort
QuickSort
HeapSort
MergeSort
Practice Big-Oh Problems


Look at each code fragment below and determine its Big-Oh notaiton. (The first three problems are taken from Fran Trees powerpoint on Big-Oh Notation)
Practice Problem 1) What is the Big-Oh of the code below?
//CODE
int sum = 0;
for(int c=1; c<=n; c++)
	for (int  c2=1; c2<=n; c2++)
		sum += c*c2;

Answer
Practice Problem 3) What is the Big-Oh of the code fragment below?
//pre-condition : n >= 2
public int doSomething(int n){
int r = 1;
   for(int m = 1 ;m < n; m++){
   r *=  m + 5;
   }
return r;
}
Answer
Practice Problem 4) What is the Big-Oh of the code fragment below?
//pre-condition : n >= 2
public int doSomething2(int n){
int r = 1;
   for(int m = 1 ;m < n; m++){
   r *= m;
   r -= 90;
   r /= 3;
   }
return r;
}
Answer
Practice Problem 5) What is the Big-Oh run time of the code below?
You can assume that the myArray1 contains elements myArray1[0], myArray1[1], myArray1[2].... myArray1[m-1] where m = myArray1.length.
int counter=0;
for(int i=0;i < m ; i++){
if(myArray1[i] != 0 )
   {
   myArray1[counter]= myArray1[i];
   counter++;
   }
 int[]  myArray2;
 myArray2 = new int[counter];

for(int i=0;i< counter; i++)
   myArray2[i] = myArray1[i];

Answer
Practice Problem 6) The problem below refers to AP Computer Science's Grid World Case Study.
What is the Big-Oh of the Grid method ArrayList<Location> getOccupiedLocations(). If you recall, this method returns an ArrayList of all the locations that are occupied. This method performs a nested loop based on the number of rows and the number of columns. This question assumes a BoundedGrid that has R rows and C columns.
Answer

Top
AddThis Social Bookmark Button