Software Development

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.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
<b>/**
 * 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];
 }
  
}
</b>
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<b>/**
 * 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;
 }
}
</b>

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:

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Charles Oundo
12 years ago

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

Back to top button