Big 0 Cheat Sheet



Big O Notation helps us determine how complex an operation is. It's of particular interest to the field of Computer Science. So for all you CS geeks out there here's a recap on the subject!

Big 0 Cheat Sheet

Big O notation cheat sheet. Big O notation is used to describe the complexity of an algorithm in terms of how well it scales. As a data set grows, so too can the number of cycles of processing timeand memory space requirements – this is known as scalability.

When you start delving into algorithms and data structures you quickly come across Big O Notation. It tells us how long an operation will take to run, as the size of the data set increases.

  • Then the total runtime of the algorithm is O(1 + N + 1).You can use algebra rules inside of Big O Notation, so final algorithm’s performance is O(N + 2). If an algorithm takes O(f(N.
  • C Data Structures and Algorithms Cheat Sheet Table of Contents 1.0 Data Structures 1.1 Overview 1.2 Vector std::vector 1.3 Deque std::deque 1.4 List std::list and std::forwardlist 1.5 Map std::map and std::unorderedmap 1.6 Set std::set 1.7 Stack std::stack 1.8 Queue std::queue 1.9 Priority Queue std::priorityqueue 1.10 Heap std::priority.

It's also a measure of complexity. The simple fact is that if something takes longer to run it is obviously more complex... logical eh? :)

Big O Notation

Here's a list of the more common Big O Notations from fastest (best) to slowest (worst).

Notation Description
O(1)Constant: operations occur at the same speed no matter the size of the data set.

ie. doing a null check

O(log n)Logarithmic: operations take slightly longer as the size of the data set increases in orders of magnitude. Very close to O(1).

ie. finding an item in a sorted data set using a binary search

O(n)Linear: operations take longer in direct proportion to the size of the data set.

ie. adding up all the values in data set

O(n log n)Linearithmic: as the data set doubles in size, operations take longer by an increasing multiplier of one.

ie. a quick sort

O(n2)Quadratic: as the data set doubles in size, operations take four times longer.

ie. two nested loops over a data set

O(2n)Exponential: for every new element added to the data set operations take twice as long.

ie. brute force attacking a password

O(n!)Factorial: operations take n! times longer in direct proportion to the size of the data set.

ie. calculating fibonacci numbers recursively

They say a picture is worth a thousand words so this should help greatly with your understanding of the above :)

Conclusion

The key take away here is that if you are working with large data sets you need to be very careful when selecting the data structure and/or algorithm you use.

I'm going to be starting a new series on Data Structures soon which will reference these Big O notations. That'll help show the real world application of these theoretical concepts.

Until then... happy coding!

Sorting Algorithms
Sorting Algorithms Space complexityTime complexity
Worst caseBest caseAverage caseWorst case
Insertion SortO(1)O(n)O(n2) O(n2)
Selection SortO(1)O(n2) O(n2) O(n2)
Smooth SortO(1)O(n)O(n log n)O(n log n)
Bubble SortO(1)O(n)O(n2) O(n2)
Shell SortO(1)O(n)O(n log n2) O(n log n2)
MergesortO(n)O(n log n)O(n log n)O(n log n)
QuicksortO(log n)O(n log n)O(n log n)O(n log n)
HeapsortO(1)O(n log n)O(n log n)O(n log n)
Data Structures Comparison
Data Structures Average CaseWorst Case
SearchInsertDeleteSearchInsertDelete
ArrayO(n)N/AN/AO(n)N/AN/A
Sorted ArrayO(log n)O(n)O(n)O(log n)O(n)O(n)
Linked ListO(n)O(1)O(1)O(n)O(1)O(1)
Doubly Linked ListO(n)O(1)O(1)O(n)O(1)O(1)
StackO(n)O(1)O(1)O(n)O(1)O(1)
Hash tableO(1)O(1)O(1)O(n)O(n)O(n)
Binary Search TreeO(log n)O(log n)O(log n)O(n)O(n)O(n)
B-TreeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)
Red-Black treeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)
AVL TreeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)

Big 0 Cheat Sheet

Growth Rates
n f(n)log nnn log nn22nn!
100.003ns0.01ns0.033ns0.1ns1ns3.65ms
200.004ns0.02ns0.086ns0.4ns1ms77years
300.005ns0.03ns0.147ns0.9ns1sec8.4x1015yrs
400.005ns0.04ns0.213ns1.6ns18.3min--
500.006ns0.05ns0.282ns2.5ns13days--
1000.070.1ns0.644ns0.10ns4x1013yrs --
1,0000.010ns1.00ns9.966ns1ms----
10,0000.013ns10ns130ns100ms----
100,0000.017ns0.10ms1.67ms10sec----
1'000,0000.020ns1ms19.93ms16.7min----
10'000,0000.023ns0.01sec0.23ms1.16days----
100'000,0000.027ns0.10sec2.66sec115.7days----
1,000'000,0000.030ns1sec29.90sec31.7 years----