Big O - Cheat Sheet

Going to make myself a personal copy of a Big O cheat sheet, and try to explain why each data structure has certain properties to be a refresher for my data structure.

Array (Dynamic)

Access - O(1)

Array's have quick access to a specified index because arrays take a finite contiguous block or memory that can be used to pinpoint memory locations.

Search - O(n)

Array's have linear time search because there are no special properties to indicate the value you are trying to look for, therefore you have to search through the elements individually.

Insertion - O(n)

Array's take up a contiguous block of memory, therefore when a new element is added to the list, it must be recreated, and/or the elements may be shifted.

Deletion - O(n)

Same as insertion in that it takes a contiguous block of memory, and therefore needs to be shifted.

Space - O(n)

Requires as much space as elements

Stack

Access -  O(n)

Must traverse through the top reference to get to index.

Search - O(n)

Must traverse through top reference to get to search value.

Insertion - O(1)

All you need to do is change reference links.

Deletion - O(1)

All you need to do is change the reference links.

Space - O(n)

Takes up as much space as its elements.

Queue

Access -  O(n)

Must traverse through the head/tail reference to get to index.

Search - O(n)

Must traverse through head/tail reference to get to search value.

Insertion - O(1)

All you need to do is change reference links.

Deletion - O(1)

All you need to do is change the reference links.

Space - O(n)

Takes up as much space as its elements.

Hashtable

Access - N/A

There is no concept of indexes in hashtables. 

Search - Average O(1), Worst O(n)

Searching is quick because of hash function computation.

Insertion - Average O(1), Worst O(n)

Hashtables know where to add elements from the hash function, but there is a possibility to hashing strategy by have worst case collisions.

Deletion - Average O(1), Worst O(n)

Hashtables know where to delete elements from the hash function, but there is a possibility to hashing strategy by have worst case collisions.

Space - O(n)

Hashtables take up as much space as its elements.

Binary Search Tree

Access - Average O(log n), Worst O(n)

Tree structure allows you to traverse a tree in log n fashion.

Search - Average O(log n), Worst O(n)

Tree structure allows you to traverse a tree in log n fashion.

Insertion - Average O(log n), Worst O(n)

Tree structure allows you to traverse a tree in log n fashion.

Deletion - Average O(log n), Worst O(n)

Tree structure allows you to traverse a tree in log n fashion.

Space - O(n)

Takes up as much space as its elements

Comments

Popular posts from this blog

Uncle Bob's Clean Architecture

C4 Model: Describing Software Architecture

Multi-Tenant Design: The Basics