Data Structures And Algorithms Cheat Sheet



When measuring the efficiency of an algorithm, we usually take into account the time and space complexity. In this article, we will glimpse those factors on some sorting algorithms and data structures, also we take a look at the growth rate of those operations.

Big-O Complexity Chart

First, we consider the growth rate of some familiar operations, based on this chart, we can visualize the difference of an algorithm with O(1) when compared with O(n2). As the input larger and larger, the growth rate of some operations stays steady, but some grow further as a straight line, some operations in the rest part grow as exponential, quadratic, factorial.

Sorting Algorithms

DATA STRUCTURES CHEAT SHEET Python - Data Structure It is a way of organizing data that contains the items stored and their relationship to each other The areas in which Data Structures are applied:. Compiler design. Operating system. Database Management System. Statistical Analysis Package. Numerical Analysis. Graphics. Algorithms and Data Structures Cheatsheet We summarize the performance characteristics of classic algorithms and data structures for sorting, priority queues, symbol tables, and graph processing. C Data Structures and Algorithms Cheat Sheet. Basic Types of Algorithms. Data Structure Basics. Efficient Sorting Basics. Below is a poster which gives a simplified overview of Data Structures and Algorithms and contains most of the import stuff to remember (for interviews or exams). Unlike the Big-O-Cheat-Sheet's paid offering, you can download it in high resolution here for free (don't waste $14 dollars)!

Data Structures And Algorithms Cheat Sheet

In order to have a good comparison between different algorithms we can compare based on the resources it uses: how much time it needs to complete, how much memory it uses to solve a problem or how many operations it must do in order to solve the problem:

  • Time efficiency: a measure of the amount of time an algorithm takes to solve a problem.
  • Space efficiency: a measure of the amount of memory an algorithm needs to solve a problem.
  • Complexity theory: a study of algorithm performance based on cost functions of statement counts.
Sorting AlgorithmsSpace ComplexityTime Complexity
Worst case Best case Average case Worst case

Bubble Sort
O(1)O(n)O(n2)O(n2)
HeapsortO(1)O(n log n)O(n log n)O(n log n)
Insertion SortO(1)O(n)O(n2)O(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)
Selection SortO(1)O(n2)O(n2)O(n2)
ShellSortO(1)O(n)O(n log n2)O(n log n2)
Smooth SortO(1)O(n)O(n log n)O(n log n)
Tree SortO(n)O(n log n)O(n log n)O(n2)
Counting SortO(k)O(n + k)O(n + k)O(n + k)
CubesortO(n)O(n)O(n log n)O(n log n)

Data Structure Operations

In this chart, we consult some popular data structures such as Array, Binary Tree, Linked-List with 3 operations Search, Insert and Delete.

Data StructuresAverage CaseWorst Case
SearchInsertDeleteSearchInsertDelete
ArrayO(n)N/AN/AO(n)N/AN/A
AVL TreeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)
B-TreeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)
Binary SearchTreeO(log n)O(log n)O(log n)O(n)O(n)O(n)
Doubly Linked ListO(n)O(1)O(1)O(n)O(1)O(1)
Hash tableO(1)O(1)O(1)O(n)O(n)O(n)
Linked ListO(n)O(1)O(1)O(n)O(1)O(1)
Red-Black treeO(log n)O(log n)O(log n)O(log n)O(log n)O(log n)
Sorted ArrayO(log n)O(n)O(n)O(log n)O(n)O(n)
StackO(n)O(1)O(1)O(n)O(1)O(1)

Growth of Functions

The order of growth of the running time of an algorithm gives a simple characterization of the algorithm’s efficiency and also allows us to compare the relative performance of alternative algorithms.

Below we have the function n f(n) with n as an input, and beside it we have some operations which take input n and return the total time to calculate some specific inputs.

n f(n)log nnn log nn22nn!
100.003ns0.01ns0.033ns0.1ns1ns3.65ms
200.004ns0.02ns0.086ns0.4ns1ms77years
300.005ns0.03ns0.147ns0.9ns1sec8.4×1015yrs
400.005ns0.04ns0.213ns1.6ns18.3min
500.006ns0.05ns0.282ns2.5ns13days
1000.070.1ns0.644ns0.10ns4×1013yrs
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

I wanted a concise, comprehensive, and correct cheat sheet for a quick review for technical interviews but couldn't find a satisfactory one online, thus I created my own. Runtime refers to average runtime. A Mark-down version can be found here. Feel free to fork it, and modify it as you like. Please comment for mistakes or important concepts missed

Data Structures

1 Dynamic Array

Sequentially stored data in a continuous chunk of memory. Double current capacity whenever capacity reached. When increasing capacity, it allocates new chunk of memory and copy over the previous values to new location.

Runtime

  • indexing: O(1)
  • searching: O(n)
  • insertion: O(n)
  • push: O(n) Note: O(1) Amortized
  • deletion: O(n)

2 Linked List

Most commonly refers to singly linked list. Data stored in nodes where each node has a reference to the next node. There are doubly linked list and circular linked list as well.

Runtime

  • indexing: O(n)
  • searching: O(n)
  • insertion: O(1)
  • deletion: O(1)

Stack and queue are often implemented with linked list because linked list are most performant for insertion/deletion, which are the most frequently used operations for stacks/queues.

Stack Last in First Out (LIFO)

Queue First in First Out (FIFO)

3 Hash Table (Hash Map)

A usually unordered data structure that maps keys to values. A hash (preferably unique) is computed for a given key and its value will be stored in the corresponding bins or index according to the hash. Internally the bins can be an array. Collision can happen when multiple keys are mapped to the same hash. Common resolution is to store a list/linked-list at each bin/index location (called chaining).

Runtime

  • value lookup: O(1)
  • insertion: O(1)
  • deletion: O(1)

4 Binary Search Tree (BST)

A binary tree with extra condition that each node is greater than or equal to all nodes in left sub-tree, and smaller than or equal to all nodes in right sub-tree.

Runtime

  • searching: O(log n)
  • insertion: O(log n)
  • deletion: O(log n)

5 Heap (Max Heap/Min Heap)

A binary tree with the condition that parent node's value is bigger/smaller than its children. So root is the maximum in a max heap and minimum in min heap. Priority queue is also referred to as heap because it's usually implemented by a heap.

Runtime

Sorting Algorithms Cheat Sheet

  • min/max: O(1)
  • insertion: O(log n)
  • deletion: O(log n)

Algorithms

1 Sorting

Bubble Sort Iterate through entire list while comparing pairs and swap positions based on their values until all elements sorted.

  • O(n^2), but fast if list is almost sorted.

Insertion Sort Iterates through unsorted list while building a sorted list. For each value encountered in unsorted list, find appropriate place in sorted list and insert it.

  • O(n^2).

Merge Sort A type of divide and conquer algorithm: 1) divides the list into two equally sized sub lists 2) sort each sub list 3) merge two sorted lists into final list.

  • O(n log n) - needs to divide log n times, and for each divide, it needs to go through all n items to merge, thus n times log n.

Heap Sort 1) Build a heap (min or max) from the unsorted list 2)repeatedly remove the root node from the heap and put into the sorted list.

  • O(n log n) - remove root node is O(log n), and has to be repeated for each node, thus n times log n.

Quick Sort A type of divide and conquer algorithm: 1) pick an item in the unsorted list as pivot 2) divided list into 2 sub lists, one contains elements smaller than pivot while the other contains elements greater than the pivot 3) sort the sub lists, and combine the results into final list

Data Structures And Algorithms Cheat Sheet Python

  • O(n log n) - need to divide O(log n) times, and after each divide, the partitioning has to go through all elements, thus overall runtime n times log n.

2 Searching

Linear SearchO(n)

Binary Search O(log n)

Breadth-First-Search (BFS) Siblings first then children. Use queue usually.

Depth-First-Search (DFS) Children first then siblings. Use stack usually.

Data Structures And Algorithms Cheat Sheet

A* Search Goal is to find the shortest path between 2 nodes in a graph. It's a best-first search. At each iteration it finds the next node to extend the path based on the criteria g(next) + h(next) where g is the distance from next node to starting node and h is the heuristic (estimated) distance of next node to final node. use a heap usually.

Computer Science Data Structures And Algorithms Cheat Sheet

3 Tree Traversals

Inorder (Left, Root, Right): useful for getting sorted list out of BST

Data Structures And Algorithms Cheat Sheet 2

Preorder (Root, Left, Right): useful for making copy of binary trees, or evaluate expression trees.

Postorder (Left, Right, Root): useful for deleting trees (because need to delete children before deleting parent)