Data Structures & Algorithms

Data Structures & Algorithms

Data Structures & Algorithms

High-Yield Revision Hub

Master Data Structures & Algorithms

Comprehensive guide to linear data structures (arrays, linked lists, stacks, queues), non-linear data structures (trees, BST, heaps, graphs), sorting & searching algorithms, and asymptotic complexity analysis.

Concept Breakdown

Detailed technical explanation

Data Structures & Algorithms (DSA) Master Revision Cheatsheet

Target Exam: Competitive Programming & CS Entrance Exams (GATE, RPSC Computer Inspector, Technical Officer)
Subject: Data Structures & Algorithms
Topic Page: https://upscorer.in/topics/data-structures-algorithms


🌟 Key Concepts & Core Rules ⭐⭐⭐⭐⭐

1. Asymptotic Notations & Complexity Analysis

  • Big-O (OO): Upper bound of execution time (Worst-case performance guarantee).
  • Big-Omega (Ω\Omega): Lower bound of execution time (Best-case performance guarantee).
  • Big-Theta (Θ\Theta): Tight bound (Average & exact growth rate when upper and lower bounds match).
  • Growth Rate Hierarchy: O(1)<O(logn)<O(n)<O(nlogn)<O(n2)<O(n3)<O(2n)<O(n!)\mathcal{O}(1) < \mathcal{O}(\log n) < \mathcal{O}(n) < \mathcal{O}(n \log n) < \mathcal{O}(n^2) < \mathcal{O}(n^3) < \mathcal{O}(2^n) < \mathcal{O}(n!)

2. Linear Data Structures

A. Arrays

  • Memory Layout: Contiguous memory allocation.
  • Access Time: O(1)\mathcal{O}(1) via index arithmetic: Address(A[i])=Base+i×Size\text{Address}(A[i]) = \text{Base} + i \times \text{Size}.
  • Insertion/Deletion: O(n)\mathcal{O}(n) worst-case due to element shifting.

B. Linked Lists

  • Structure: Nodes containing Data + Pointer to next node.
  • Access Time: O(n)\mathcal{O}(n) sequential traversal.
  • Insertion/Deletion: O(1)\mathcal{O}(1) at known pointer location (no shifting required).
  • Variants: Singly Linked List, Doubly Linked List (O(1)\mathcal{O}(1) backward traversal), Circular Linked List (last node points to head).

C. Stacks

  • Principle: Last-In, First-Out (LIFO).
  • Primary Operations: push(x) O(1)\mathcal{O}(1), pop() O(1)\mathcal{O}(1), peek() O(1)\mathcal{O}(1).
  • Key Applications: Function call stack, Infix to Postfix/Prefix evaluation, Balancing Parentheses, Backtracking.

D. Queues

  • Principle: First-In, First-Out (FIFO).
  • Primary Operations: enqueue(x) O(1)\mathcal{O}(1), dequeue() O(1)\mathcal{O}(1).
  • Circular Queue: Solves space wastage of array queue using modulo arithmetic: rear=(rear+1)(modN)\text{rear} = (\text{rear} + 1) \pmod N.
  • Applications: CPU Scheduling (Round Robin), BFS Graph Traversal, Buffer management.

3. Non-Linear Data Structures

A. Trees & Binary Search Trees (BST)

  • Binary Tree: Each node has at most 2 children.
  • Tree Traversals:
    • Inorder (Left, Root, Right): Returns sorted sequence for BST!
    • Preorder (Root, Left, Right): Used for expression trees and tree duplication.
    • Postorder (Left, Right, Root): Used for tree deletion and Postfix generation.
    • Level-Order: Breadth-First search using Queue.
  • BST Property: Left Child<RootRight Child\text{Left Child} < \text{Root} \le \text{Right Child}.
    • Search, Insert, Delete Time Complexity: O(h)\mathcal{O}(h) where hh is tree height (O(logn)\mathcal{O}(\log n) balanced, O(n)\mathcal{O}(n) skewed).

B. Binary Heaps

  • Complete Binary Tree stored in array (ii's left child = 2i+12i+1, right child = 2i+22i+2, parent = (i1)/2\lfloor(i-1)/2\rfloor).
  • Min-Heap: Parent \le Children. Max-Heap: Parent \ge Children.
  • Heap Operations: Insert O(logn)\mathcal{O}(\log n), Extract Min/Max O(logn)\mathcal{O}(\log n), Heapify array O(n)\mathcal{O}(n).

C. Graphs

  • Representations: Adjacency Matrix (O(V2)\mathcal{O}(V^2) space) vs Adjacency List (O(V+E)\mathcal{O}(V + E) space).
  • Graph Traversals:
    • BFS: Level-by-level using Queue. Time: O(V+E)\mathcal{O}(V + E). Finds shortest path in unweighted graphs.
    • DFS: Deep exploration using Stack / Recursion. Time: O(V+E)\mathcal{O}(V + E). Used for Topological Sorting and Cycle Detection.

4. Sorting & Searching Algorithms Summary Table

AlgorithmBest TimeAvg TimeWorst TimeSpaceStable?
Linear SearchO(1)\mathcal{O}(1)O(n)\mathcal{O}(n)O(n)\mathcal{O}(n)O(1)\mathcal{O}(1)Yes
Binary SearchO(1)\mathcal{O}(1)O(logn)\mathcal{O}(\log n)O(logn)\mathcal{O}(\log n)O(1)\mathcal{O}(1)Yes
Bubble SortO(n)\mathcal{O}(n)O(n2)\mathcal{O}(n^2)O(n2)\mathcal{O}(n^2)O(1)\mathcal{O}(1)Yes
Selection SortO(n2)\mathcal{O}(n^2)O(n2)\mathcal{O}(n^2)O(n2)\mathcal{O}(n^2)O(1)\mathcal{O}(1)No
Insertion SortO(n)\mathcal{O}(n)O(n2)\mathcal{O}(n^2)O(n2)\mathcal{O}(n^2)O(1)\mathcal{O}(1)Yes
Merge SortO(nlogn)\mathcal{O}(n \log n)O(nlogn)\mathcal{O}(n \log n)O(nlogn)\mathcal{O}(n \log n)O(n)\mathcal{O}(n)Yes
Quick SortO(nlogn)\mathcal{O}(n \log n)O(nlogn)\mathcal{O}(n \log n)O(n2)\mathcal{O}(n^2)O(logn)\mathcal{O}(\log n)No
Heap SortO(nlogn)\mathcal{O}(n \log n)O(nlogn)\mathcal{O}(n \log n)O(nlogn)\mathcal{O}(n \log n)O(1)\mathcal{O}(1)No

🚨 Common Exam Pitfalls & Misconceptions

  1. Array Indexing vs Element Shift: Direct index access in array is O(1)\mathcal{O}(1), but insertion/deletion requires shifting O(n)\mathcal{O}(n) elements.
  2. Inorder Traversal of BST: Inorder traversal of any Binary Search Tree ALWAYS yields elements in strictly sorted ascending order.
  3. Queue Space Wastage: Linear array queue runs out of space when rear = N-1 even if front elements are dequeued. Use Circular Queue with modulo arithmetic (rear+1)(modN)(\text{rear} + 1) \pmod N.
  4. Binary Search Requirement: Binary Search CANNOT be performed on an unsorted array or a standard Singly Linked List in O(logn)\mathcal{O}(\log n) time due to lack of random access.
  5. Quick Sort Worst Case: Quick sort degenerates to O(n2)\mathcal{O}(n^2) time complexity when the pivot is chosen poorly on an already sorted array.
  6. Heap Build Time: Building a heap from an unsorted array of size nn takes O(n)\mathcal{O}(n) time using Floyd's algorithm, NOT O(nlogn)\mathcal{O}(n \log n).
  7. Stack Overflow vs Underflow: Stack Overflow occurs when push() is called on a full stack (top == MAX-1). Stack Underflow occurs when pop() is called on an empty stack (top == -1).
  8. BFS Data Structure: BFS requires a Queue, whereas DFS requires a Stack (or recursive call stack).
  9. Stability in Sorting: A sorting algorithm is stable if two elements with equal keys appear in the same relative order in output as in input (e.g. Merge Sort is stable, Quick Sort & Heap Sort are unstable).
  10. Recursion Space: Recursive algorithms require O(h)\mathcal{O}(h) auxiliary call stack space where hh is maximum recursion depth.

🇮🇳 हिंदी मास्टर सारांश (Hindi Revision Summary)

डेटा स्ट्रक्चर्स एवं एल्गोरिदम (Data Structures & Algorithms)

  • ऐरे (Array): लगातार (Contiguous) मेमोरी लोकेशन पर डेटा स्टोर करता है। इंडेक्स द्वारा एक्सेस टाइम O(1)\mathcal{O}(1) होता है।
  • लिंक्ड लिस्ट (Linked List): नोड्स में डेटा और अगले नोड का पॉइंटर होता है। एलिमेंट्स को खिसकाए बिना O(1)\mathcal{O}(1) में इंसर्ट/डिलीट संभव है।
  • स्टैक (Stack): LIFO (Last-In, First-Out) सिद्धांत पर कार्य करता है। मुख्य ऑपरेशन्स: Push, Pop, Peek (O(1)\mathcal{O}(1))।
  • क्यू (Queue): FIFO (First-In, First-Out) सिद्धांत पर कार्य करता है। सर्कुलर क्यू (Circular Queue) मेमोरी की बर्बादी रोकता है: (rear+1)(modN)(\text{rear}+1) \pmod N
  • बाइनरी सर्च ट्री (BST): बायां बच्चा < रूट \le दायां बच्चा। इनऑर्डर (Inorder) ट्रैवर्सल हमेशा सॉर्टेड (Sorted) क्रम देता है।
  • बाइनरी सर्च (Binary Search): केवल सॉर्टेड ऐरे पर कार्य करता है। समय जटिलता (Time Complexity) O(logn)\mathcal{O}(\log n) है।
  • सॉर्टिंग जटिलता:
    • Merge Sort: O(nlogn)\mathcal{O}(n \log n) (सर्वदा स्थाई/Stable)।
    • Quick Sort: औसतन O(nlogn)\mathcal{O}(n \log n), वर्स्ट केस O(n2)\mathcal{O}(n^2) (अस्थाई/Unstable)।
    • Heap Sort: O(nlogn)\mathcal{O}(n \log n) (इन-प्लेस/In-Place, O(1)\mathcal{O}(1) अतिरिक्त स्पेस)।

Key Revision Rules

Essential formulas and core points to memorize

  • 1⚡ Asymptotic Growth Order: O(1) < O(log n) < O(n) < O(n log n) < O(n^2) < O(n^3) < O(2^n) < O(n!).
  • 2🔢 Array Memory Access: Contiguous memory allocation allows O(1) direct access by index calculation: Address(A[i]) = Base + i * Size.
  • 3🔗 Linked List Operations: Sequential access O(n), but insertion and deletion at a known node position takes O(1) time without shifting elements.
  • 4🥞 Stack (LIFO): Last-In First-Out structure. Key operations: push(x) O(1), pop() O(1), peek() O(1). Applications: Function recursion stack, expression evaluation (Infix to Postfix).
  • 5🧍 Queue (FIFO): First-In First-Out structure. Key operations: enqueue(x) O(1), dequeue() O(1). Circular Queue uses modulo math (rear+1)%N to prevent memory wastage.
  • 6🌳 Binary Search Tree (BST): Left Child < Root <= Right Child. Inorder traversal of a BST ALWAYS yields elements in strictly sorted ascending order.
  • 7📊 BST Operations: Search, Insert, and Delete take O(h) time, where h is height of tree (O(log n) for balanced BST, O(n) for skewed tree).
  • 8⛰️ Binary Heap: Complete binary tree represented in array. Heapify unsorted array takes O(n) time. Insertion & Extraction take O(log n) time.
  • 9🕸️ Graph Representations & Traversals: Adjacency List uses O(V+E) space. BFS uses Queue for level-order exploration. DFS uses Stack/Recursion for deep traversal.
  • 10🔍 Linear vs Binary Search: Linear Search takes O(n) time on any array. Binary Search takes O(log n) time but REQUIRES a sorted array with random access.
  • 11🔄 Merge Sort vs Quick Sort: Merge Sort takes guaranteed O(n log n) time and is Stable. Quick Sort takes average O(n log n) but worst-case O(n^2) and is Unstable.
  • 12⚖️ Sorting Stability: A sorting algorithm is Stable if equal keys preserve their relative input order (Merge Sort, Insertion Sort, Bubble Sort).

Common Exam Mistakes

Where students frequently lose marks

Mistake 1: Confusing Array Index Access with Element Insertion. Array indexing is O(1), but inserting/deleting requires shifting elements taking O(n) time.
Mistake 2: Forgetting BST Inorder Traversal Property. Inorder traversal of any Binary Search Tree ALWAYS produces a sorted sequence.
Mistake 3: Memory Wastage in Linear Array Queue. Front element deletion leaves unused space; Circular Queue with (rear+1)%N solves this.
Mistake 4: Attempting Binary Search on Singly Linked List in O(log n). Singly Linked Lists lack random access, making Binary Search O(n).
Mistake 5: Assuming Quick Sort is Always O(n log n). Poor pivot selection on already sorted arrays causes Quick Sort to degrade to O(n^2).
Mistake 6: Thinking Heapifying an Array takes O(n log n). Floyd's Heapify algorithm builds a heap from an array in linear O(n) time.
Mistake 7: Misunderstanding Stack Overflow vs Underflow. Overflow happens when pushing onto a full stack; Underflow happens when popping from an empty stack.
Mistake 8: Swapping BFS and DFS Data Structures. BFS requires a Queue (FIFO), while DFS requires a Stack (LIFO or Recursion).
Mistake 9: Misunderstanding Sorting Stability. Stable sorting preserves original relative order of equal keys (e.g. Merge Sort is stable, Quick Sort is not).
Mistake 10: Ignoring Auxiliary Call Stack Space in Recursion. Recursive algorithms incur O(h) extra space where h is maximum recursion depth.

Topic Quiz Practice

1 of 10
Question 1

टेक्स्ट एन्क्रिप्शन(Text's encryption) टेक्स्ट का Blank है -