The simple Big-O Notation Post

Our JCG partner Brian Du Preez has posted an explanatory article on his blog, Zen in the art of IT, concerning the Big-O notation. The Big-O notation is used for describing algorithm performance, scalability, execution and complexity factors. Understanding what the Big-O notation stands for is essential for every developer who wants to write performant, robust and scalable code.

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.)

nConstant
O(1)

Logarithmic
O(log n)

Linear
O(n)
Linear Logarithmic
O(n log n)
Quadractic
O(n2)
Cubic
O(n3)
1111111
2112248
412481664
81382464512
161416642564,096
1,0241101,02410,2401,048,5761,073,741,824
1,048,5761201,048,57620,971,52010121016

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:

Structuregetaddremovecontains
ArrayListO(1)O(1)O(n)O(n)
LinkedListO(n)O(1)O(1)O(n)
HashSetO(1)O(1)O(1)O(1)
LinkedHashSetO(1)O(1)O(1)O(1)
TreeSetO(log n)O(log n)O(log n)O(log n)



Maps:

StructuregetputremovecontainsKey
HashMapO(1)O(1)O(1)O(1)
LinkedHashMapO(1)O(1)O(1)O(1)
TreeMapO(log n)O(log n)O(log n)O(log n)

Better is the enemy of good!

Byron

Related Articles:

Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

One Response to "The simple Big-O Notation Post"

  1. a java program that 3 numbers from the keyboard and computes their mean and product

Leave a Reply


− 2 = three



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books