Big O Notation Python Cheat Sheet



Apr 9, 2018 - About the #data-structures series The #data-structures series is a collection of posts about reimplemented data structures in JavaScript. If you are not familiar with data structures, a quick introduction and the full list of reimplemented data structures can be found in the introduction post of the series on data structures in JavaScript. If you feel comfortable with the concept. Know Thy Complexities! This webpage covers the space and time Big-O complexities of common algorithms used in Computer Science. When preparing for technical interviews in the past, I found myself spending hours crawling the internet putting together the best, average, and worst case complexities for search and sorting algorithms so that I wouldn't be stumped when asked about them.

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)

Python Big O Notation Examples

Big o notation examples

See All Results For This Question

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----
See full list on datacamp.com

In this section we are going to review searching and sorting algorithms.

Why is sorting so important

  • The first step in organizing data is sorting. Lots of tasks become easier once a data set of items is sorted
  • Some algorithms like binary search are built around a sorted data structure.
  • In accordance to S. Skiena computers have historically spent more time sorting than doing anything else. Sorting remains the most ubiquitous combinatorial algorithm problem in practice.
Big o notation examples

Considerations:

Python - How Do I Calculate Time Complexity For This ...

  • How to sort: descending order or ascending order?
  • Sorting based on what? An object name (alphabetically), by some number defined by its fields/instance variables. Or maybe compare dates, birthdays, etc.
  • What happens with equals keys, for example various people with the same name: John, then sort them by Last Name.
  • Does your sorting algorithm sorts in place or needs extra memory to hold another copy of the array to be sorted. This is even more important in embedded systems.

Java uses Comparable interface for sorting and returns: +1 if compared object is greater, -1 if compared object is less and 0 if compared objects are equal.

Sorting becomes more ubiquitous when we think on all the things we do daily that are previously sorted for us to understand and have better access to them:

  • Imagine trying to find a phone number in an unsorted phone book, or searching for a word in an unsorted dictionary.
  • Your MP3 player can sort your lists by artists name, genre, song name, ratings.
  • Search engines display results in descending order of importance
  • Spreadsheets can be sorted in various ways to work better with their contents

Python Big O Notation

Searching

There are two types of searching algorithms: Those that need a previously ordered data structure in order to work properly, and those that don’t need an ordered list.

Big 0 Notation Cheat Sheet

Searching is also very important for many computing applications: searching through a search engine, finding a bank account balance for some client, searching in a large data set for a particular value, searching in your directories for some needed file. Many applications rely on effective search, if your application is complete but takes long too perform a search and retrieve data it will be discarded as useless.