Comprehensive Data Structures and Algorithms Study Notes

Classification and Foundations of Data Structures
  • Definition: A data structure is a method of organizing, storing, and managing data in computer memory to enable efficient access, modification, and processing.


Classification of Data Structures
  • Primitive vs. Non-Primitive:

    • Primitive: Built-in data types provided directly by the programming language storing a single value with fixed memory (e.g., int, char, float, double, boolean).

    • Non-Primitive: Derived structures designed to store multiple values (e.g., Array, Linked List, Stack, Queue, Tree, Graph).

  • Static vs. Dynamic:

    • Static: Size is fixed and allocated at compile time; provides faster indexed access but risks wasted memory and cannot grow during execution (e.g., Array).

    • Dynamic: Memory is allocated at run time; size flexibly expands or shrinks during execution, but requires pointer management and extra pointer storage (e.g., Linked List).

  • Linear vs. Non-Linear:

    • Linear: Elements are arranged sequentially on a single level with simple traversal (e.g., Array, Linked List, Stack, Queue).

    • Non-Linear: Elements are arranged hierarchically or across a multi-level network with complex traversal (e.g., Tree, Graph).

  • Primary Applications:

    • Array: Storing marks and record sets.

    • Stack: Function calls, undo operations, expression evaluation, and recursion.

    • Queue: Printer scheduling, ticket booking, and Round Robin scheduling.

    • Tree: File directory management and hierarchical database organization.

    • Graph: Computer networks, maps, and social network modeling.

Algorithm Analysis and Asymptotic Complexity
  • Algorithm Characteristics: Requires zero or more inputs, yields at least one output, possesses definiteness (clear steps), finiteness (terminates after finite steps), and effectiveness (executable operations).

  • Analysis Dimensions:

    • Time Complexity: Quantifies the growth of basic operations as input size nn increases.

    • Space Complexity: Quantifies memory usage (input space+auxiliary space\text{input space} + \text{auxiliary space}) as input size nn increases.

  • Cases of Complexity:

    • Best Case: Minimum number of operations required for execution.

    • Average Case: Expected number of operations across standard inputs.

    • Worst Case: Maximum number of operations required for execution.

  • Standard Growth Rates:

    • O(1)O(1): Constant time (e.g., accessing an array element by index).

    • O(log⁡(n))O(\log(n)): Logarithmic time (e.g., Binary Search).

    • O(n)O(n): Linear time (e.g., Linear Search, single loop).

    • O(nlog⁡(n))O(n \log(n)): Linearithmic time (e.g., Merge Sort).

    • O(n2)O(n^2): Quadratic time (e.g., Bubble Sort, nested loops).

    • O(2n)O(2^n): Exponential time (e.g., recursive Fibonacci).

Searching and Sorting Algorithms
1. Searching Techniques
  • Linear Search:

    • Sequentially compares each element from index 00 to n−1n - 1. Operates on unsorted data.

    • Complexity: Best case O(1)O(1), Average case O(n)O(n), Worst case O(n)O(n), Space complexity O(1)O(1).

    • Diagrammatic Example (Searching for 45):text Target: 45 Index: [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] Array: | 10 | | 25 | | 30 | | 45 | | 50 | | | | | Step 1: 10 != 45 | | | Step 2: 25 != 45 | | Step 3: 30 != 45 | Step 4: 45 == 45 -> Target Found at Index 3!     

  • Binary Search:

    • Operates strictly on sorted arrays by repeatedly comparing the target against mid=low+high2\text{mid} = \frac{\text{low} + \text{high}}{2} and halving the search space.

    • Complexity: Best case O(1)O(1), Average/Worst case O(log⁡(n))O(\log(n)), Space complexity O(1)O(1).

    • Diagrammatic Example (Searching for 60): ```text Target: 60 Initial: low = 0, high = 6 -> mid = (0 + 6) / 2 = 3 Index: [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ] [ 6 ] Value: | 10 | | 20 | | 30 | | 40 | | 50 | | 60 | | 70 | ^ ^ ^ low mid high Compare: arr[3] = 40 < 60 -> Search Right -> low = mid + 1 = 4

    Next: low = 4, high = 6 -> mid = (4 + 6) / 2 = 5 Index: [ 4 ] [ 5 ] [ 6 ] Value: | 50 | | 60 | | 70 | ^ ^ ^ low mid high Compare: arr[5] = 60 == 60 -> Target Found at Index 5!     ```

2. Elementary Sorting Algorithms
  • Bubble Sort:

    • Repeatedly compares adjacent elements and swaps them if out of order, bubbling the largest unsorted element to the end.

    • Complexity: Total comparisons n(n−1)2\frac{n(n - 1)}{2}. Best case O(n)O(n), Average/Worst case O(n2)O(n^2), Space O(1)O(1); stable and in-place.

    • Diagrammatic Example (Sorting [5, 1, 4, 2, 8]): ```text Pass 1: (5 > 1) -> Swap -> [ 1, 5, 4, 2, 8 ] (5 > 4) -> Swap -> [ 1, 4, 5, 2, 8 ] (5 > 2) -> Swap -> [ 1, 4, 2, 5, 8 ] (5 < 8) -> No Swap -> [ 1, 4, 2, 5 | 8 ] (8 is locked)

    Pass 2: (1 < 4) -> No Swap (4 > 2) -> Swap -> [ 1, 2, 4, 5 | 8 ] (4 < 5) -> No Swap -> [ 1, 2, 4 | 5, 8 ] (5 is locked)

    Pass 3: (1 < 2), (2 < 4) -> No Swaps needed -> Sorted: [ 1, 2, 4, 5, 8 ]     ```

  • Selection Sort:

    • Locates the minimum element in the unsorted section and executes exactly one swap per pass to place it at the front.

    • Complexity: Best/Average/Worst case O(n2)O(n^2), Space O(1)O(1); in-place.

    • Diagrammatic Example (Sorting [64, 25, 12, 22, 11]):text Initial: [ 64, 25, 12, 22, 11 ] Pass 1: Min = 11 (index 4) -> Swap with arr[0] (64) -> [ 11 | 25, 12, 22, 64 ] Pass 2: Min = 12 (index 2) -> Swap with arr[1] (25) -> [ 11, 12 | 25, 22, 64 ] Pass 3: Min = 22 (index 3) -> Swap with arr[2] (25) -> [ 11, 12, 22 | 25, 64 ] Pass 4: Min = 25 (index 3) -> In place -> [ 11, 12, 22, 25, 64 ] (Sorted) &nbsp;&nbsp;&nbsp;&nbsp;

  • Insertion Sort:

    • Builds the sorted array element by element by comparing the current key against predecessor elements, shifting larger elements right, and inserting the key into position.

    • Complexity: Best case O(n)O(n), Average/Worst case O(n2)O(n^2), Space O(1)O(1); stable and in-place.

    • Diagrammatic Example (Sorting [5, 2, 4, 6, 1]):text Pass 1: Key = 2 -> (5 > 2) Shift 5 right -> Insert 2 [ 2, 5 | 4, 6, 1 ] Pass 2: Key = 4 -> (5 > 4) Shift 5 right -> Insert 4 [ 2, 4, 5 | 6, 1 ] Pass 3: Key = 6 -> (6 > 5) No Shift [ 2, 4, 5, 6 | 1 ] Pass 4: Key = 1 -> Shift 6, 5, 4, 2 right -> Insert 1 at index 0 [ 1, 2, 4, 5, 6 ] (Sorted) &nbsp;&nbsp;&nbsp;&nbsp;

3. Advanced Sorting Algorithms
  • Merge Sort:

    • Divide-and-conquer algorithm dividing the array into halves until singletons remain, then merging sorted subarrays.

    • Complexity: Best/Average/Worst case O(nlog⁡(n))O(n \log(n)), Space O(n)O(n) auxiliary memory; stable, not in-place.

    • Diagrammatic Example (Sorting [38, 27, 43, 3]):text [ 38, 27, 43, 3 ] / \ [ 38, 27 ] [ 43, 3 ] (Divide) / \ / \ [ 38 ] [ 27 ] [ 43 ] [ 3 ] (Singletons) \ / \ / [ 27, 38 ] [ 3, 43 ] (Merge Subarrays) \ / [ 3, 27, 38, 43 ] (Final Sorted) &nbsp;&nbsp;&nbsp;&nbsp;

  • Quick Sort:

    • Selects a pivot, partitions the array so elements smaller than the pivot appear to the left and larger to the right, and recurses on both partitions.

    • Complexity: Best/Average case O(nlog⁡(n))O(n \log(n)), Worst case O(n2)O(n^2), Space O(log⁡(n))O(\log(n)) recursive stack; in-place, unstable.

    • Diagrammatic Example (Sorting [10, 7, 8, 9, 1, 5], Pivot = 5):text Initial: [ 10, 7, 8, 9, 1, 5 ] (Pivot = 5) Partition: Elements < 5: [ 1 ] Pivot element: [ 5 ] Elements > 5: [ 10, 7, 8, 9 ] State: [ 1 ] + [ 5 ] + [ 10, 7, 8, 9 ] Left [ 1 ] is sorted. Right [ 10, 7, 8, 9 ] partitioned with Pivot = 9: -> [ 7, 8 ] + [ 9 ] + [ 10 ] Result: [ 1, 5, 7, 8, 9, 10 ] &nbsp;&nbsp;&nbsp;&nbsp;

  • Heap Sort:

    • Builds a Max Heap in O(n)O(n) time, then repeatedly swaps the root with the last leaf, decrements heap size, and runs heapify (O(log⁡(n))O(\log(n))).

    • Complexity: Best/Average/Worst case O(nlog⁡(n))O(n \log(n)), Space O(1)O(1); in-place, unstable.

    • Diagrammatic Example (Sorting [4, 10, 3, 5, 1]): ```text Step 1: Build Max Heap: 10 / \ 5 3 / \ 4 1 Array: [ 10, 5, 3, 4, 1 ]

    Step 2: Swap Root (10) with Last Leaf (1) -> [ 1, 5, 3, 4 | 10 ] Step 3: Heapify root (1) down -> 5 moves to root: 5 / \ 4 3 / 1 Array: [ 5, 4, 3, 1 | 10 ]

    Step 4: Repeat swap and heapify -> Final Sorted Array: [ 1, 3, 4, 5, 10 ]     ```

Hashing Techniques
  • Principles: Maps large key spaces to fixed hash table indices via a hash function h(key)=indexh(\text{key}) = \text{index} to achieve average O(1)O(1) search, insertion, and deletion.

  • Common Hash Function: Division method: h(k)=k(modm)h(k) = k \pmod m, where mm is the table size.

  • Collisions and Overflow:

    • Collision: Occurs when two distinct keys yield an identical hash value (h(k1)=h(k2)h(k_1) = h(k_2)).

    • Overflow: Occurs when the target table location is fully occupied and lacks immediate capacity for the new key.

  • Collision Resolution Methods:

    • Open Addressing (In-table probing):

    • Linear Probing: (h(k)+i)(modm)(h(k) + i) \pmod m

    • Quadratic Probing: (h(k)+i2)(modm)(h(k) + i^2) \pmod m

    • Double Hashing: (h1(k)+i×h2(k))(modm)(h_1(k) + i \times h_2(k)) \pmod m

    • Chaining (Separate Chaining):text Table Size m = 10, Keys: 25, 45, 65 -> h(k) = k mod 10 = 5 Index [ 0 ] -> NULL ... Index [ 5 ] -> [ 25 | • ] ---> [ 45 | • ] ---> [ 65 | NULL ] &nbsp;&nbsp;&nbsp;&nbsp;

Linear Data Structures: Lists, Stacks, and Queues
1. Linked Lists
  • Singly Linked List:

    • Unidirectional chain of nodes containing data and a next pointer.

    • Insertion/deletion at the head takes O(1)O(1); arbitrary position operations and searches require traversal (O(n)O(n)).

    • Diagram:text head ---> [ 10 | • ] ---> [ 20 | • ] ---> [ 30 | NULL ] &nbsp;&nbsp;&nbsp;&nbsp;

  • Doubly Linked List (DLL):

    • Nodes contain prev, data, and next pointers. Enables bidirectional traversal and simplified deletion.

    • Diagram:text NULL <--- [ • | 10 | • ] <===> [ • | 20 | • ] <===> [ • | 30 | • ] ---> NULL &nbsp;&nbsp;&nbsp;&nbsp;

  • Circular Linked List:

    • The final node points back to the initial node rather than NULL. Traversed via do-while loops until the head is reached again; used for Round Robin CPU scheduling, turn-based games, and audio playlists.