Fundamentals 8 min read

Essential Algorithm Cheat Sheet: Sorting, Graph, Greedy, and Bit Operations

This article provides a concise reference of core algorithms—including sorting methods, graph traversals, shortest‑path techniques, greedy strategies, bit‑level manipulations, and asymptotic notation—detailing their stability, time complexities, key concepts, and practical examples for interview preparation.

ITPUB
ITPUB
ITPUB
Essential Algorithm Cheat Sheet: Sorting, Graph, Greedy, and Bit Operations

Sorting Algorithms

Quick Sort – unstable. Time complexity: best O(n log n), average O(n log n), worst O(n²).

Merge Sort – stable. Time complexity: O(n log n) in all cases.

Bucket Sort – distributes elements into k buckets; each bucket is sorted (often recursively). Time complexity: best Ω(n + k), average Θ(n + k), worst O(n²).

Radix Sort – processes digits (or bits) of keys and uses bucket sort internally; no final per‑bucket sort. Time complexity: best Ω(n·k), average Θ(n·k), worst O(n·k) where k is the number of digit positions.

Quick Sort illustration
Quick Sort illustration

Graph Algorithms

Depth‑First Search (DFS) – explores a graph by going as deep as possible before backtracking. Time complexity O(|V| + |E|).

DFS illustration
DFS illustration

Breadth‑First Search (BFS) – explores vertices level by level. Time complexity O(|V| + |E|).

BFS illustration
BFS illustration

Topological Sort – linear ordering of vertices in a directed acyclic graph such that for every edge u→v, u appears before v. Time complexity O(|V| + |E|).

Dijkstra’s algorithm – single‑source shortest paths for non‑negative edge weights. Simple array implementation O(|V|²); with a binary heap O((|V|+|E|) log |V|).

Dijkstra illustration
Dijkstra illustration

Bellman‑Ford algorithm – single‑source shortest paths that also handles negative‑weight edges (detects negative cycles). Worst‑case time complexity O(|V||E|), best O(|E|) when no relaxation is needed.

Bellman‑Ford illustration
Bellman‑Ford illustration

Floyd‑Warshall algorithm – all‑pairs shortest paths in weighted graphs (negative edges allowed, no negative cycles). Time complexity O(|V|³).

Floyd‑Warshall illustration
Floyd‑Warshall illustration

Minimum Spanning Tree (MST)

Prim’s algorithm – grows the MST by repeatedly adding the cheapest edge from the tree to a vertex outside. Simple array implementation O(|V|²); with a binary heap O(|E| log |V|).

Kruskal’s algorithm – sorts all edges by weight and adds them to the forest if they do not create a cycle (using Union‑Find). Time complexity O(|E| log |V|).

Prim illustration
Prim illustration
Kruskal illustration
Kruskal illustration

Greedy Algorithms

Greedy methods make the locally optimal choice at each step. They are applicable when the problem exhibits optimal substructure and the greedy‑choice property.

Example – Coin Change (V = 41)

Choose the largest coin ≤ V (25¢). Remaining V = 16.

Choose 10¢. Remaining V = 6.

Choose 5¢. Remaining V = 1.

Choose 1¢. Remaining V = 0.

Total coins used: 4.

Bit Manipulation

Test k‑th bit: s & (1 << k) Set k‑th bit: s |= (1 << k) Clear k‑th bit: s &= ~(1 << k) Toggle k‑th bit: s ^= (1 << k) Multiply by 2ⁿ: s << n Divide by 2ⁿ: s >> n Intersection: s & t Union: s | t Difference: s & ~t Extract lowest set bit: s & (-s) Extract lowest zero bit: ~s & (s + 1) Swap two integers without a temporary variable:

x ^= y; y ^= x; x ^= y;

Asymptotic Notations

Big O (O) – upper bound describing worst‑case growth.

Little o (o) – strict upper bound; the function grows strictly slower.

Big Omega (Ω) – lower bound describing best‑case growth.

Little ω (ω) – strict lower bound; the function grows strictly faster.

Theta (Θ) – tight bound representing both upper and lower limits.

Source Repository

Algorithm implementations and interview resources are hosted at https://github.com/kdn251/interviews.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Interview PreparationAlgorithmscomplexitySortinggraph algorithmsbit manipulationgreedy
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.