Algorithms And Data Structures Cheat Sheet



O(n2)O(n^2)O(n2) O(1)O(1)O(1) Selection sort works by repeatedly 'selecting' the. Dec 14, 2020 C and Data Structures & Algorithms Cheat Sheet These are two cheat sheets I put together describing both basic C syntax (mostly C11) and many common data structures and algorithms in C, which I've used to study for my past interviews at Google, NASA, etc.

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

  • 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

  • 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)

Sorting

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.

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.

3 Tree Traversals

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

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)

Java Algorithms And Data Structures Cheat Sheet

Hey Finxters! Do you know what time it is? That’s right! It’s time for some more cheat sheets!! These cheat sheets are meant to help you on your way to becoming a great Python developer and of course becoming one of the best Python freelancers globally! This article is all about algorithms used in software development and the cheat sheets we will use to do this. Let’s get started without further delay!

Cheat Sheet 1: Princeton

This cheat sheet is one you will want to bookmark as it is part of an ebook! It primarily focuses on Algorithm and Data Structures.The area I would like you to focus is ⅓ of the way down beginning at arrays. Consider bookmarking the book(I have!) Chapter 4 deep dives into algorithms and data structures. It includes a list of the Python code structures used throughout the chapter with complete explanations on what is happening and the how!

Pros: Perfect for deep diving into Algorithm coding in Python!

Cons: Part of an ebook

Cheat Sheet 2: AlgoDaily.com

This cheat sheet will go over the concepts of Big-O and Algorithmic complexity used in programming. Plus a video that discusses the concept! Algodaily is the place to be if you want to learn Algorithms and data structures for interviews to land a software career as a consultant or full time employee for a company.

Pros: Best place to learn everything you need to know for Algorithms and Data structures!

Cons: Does not have the ability to print, more structured towards interviews.

Cheat Sheet 3: Microsoft

This cheat sheet can be downloaded and pinned to the wall behind the monitor or place in your developers binder. It is carefully structured by microsoft to show you how to properly use algorithms for ML. Start in the What do you want to do box and you will be on your way to writing your algorithm!

And

Pros: Perfect place to start. It answers the question Where do I start?

Cons: None that I can see.

Cheat Sheet 4: Cheatography

This cheat sheet is all about sorting algorithms with boiler code included for bubble sorting, quicksort and selection. It presents a clear table of which is a method and which is a sorting algorithm. Print this one and keep it pinned to the wall or place it in your developers binder

Pros: Rated ‘E’ for everyone.

Cons: None that I can see.

Cheat Sheet 5: Medium

This cheat sheet is for learning the searching and sorting algorithms used in Python. It has code snippets, visuals on the different algorithms and explanations. This cheat sheet is on Medium, a fast up-coming developers source on information in the Development and IT field. Bookmark this page, since it doesn’t print.

Pros: Great place to begin learning sorting and searching algorithms.

Cons: You’ll have to subscribe to Medium to read this cheat sheet.

Cheat Sheet 6: Dummies

Here is another cheat sheet for you to bookmark, presented to you from the classic series How to for Dummies. It has tables for looking for, which has type, explanations and links for further explanations.

Pros: Perfect if you’re having a hard time understanding where to begin with your algorithms

Cons: Cannot be printed. Bookmark the page, I did.

Algorithms And Data Structures Cheat Sheet

Cheat Sheet 7: Packt

This is a pdf that you can print and pin to the wall behind the monitor! It has tables of the different algorithms, the data structures and graphs. Keep it handy when you are learning Big-O algorithms.

Pros: Rated ‘E’ for everyone.

Cons: You’ll have to go to Packt for the Big-O book to read.

Cheat Sheet 8: Analytics Vidhya

Algorithms and data structures cheat sheet

This cheat sheet is broken down into 2 sides with Python and R for machine learning algorithms for supervised, unsupervised and reinforcement learning. It has code examples to get you started for both languages.

Pros: Rated ‘E’ for everyone, contains 2 languages.

Cons: Save it as an image to your laptop before printing.

Cheat Sheet 9: Scikit Learn

This cheat sheet map uses Scikit Learn to point you towards the right estimator to try on your data sets.

Pros: Rated ‘E’ for everyone.

Cons: No code samples.

Cheat Sheet 10: SAS

This cheat sheet is used to help point you towards the correct algorithm to use for your data sets. The tutorial found online. Which machine learning algorithm do I use will help you make the correct choice.

Pros: Rated ‘E’ for everyone.

Cons: None that I can see.

This is just some of the cheat sheets I have found online and there are a ton more!! It is important to really understand Machine learning algorithms so I encourage you to sign up for a library (Packt is great!) and read the books they have available! To get you started I added a book from Pearsons! This book is an Introduction to Programming with Python! It covers Python from its basics to the algorithms and data structures you need to get you started! Keep on becoming a great Pythoner! One code at a time!

Related Articles:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Related Posts