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.

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 increases.
Space Complexity: Quantifies memory usage () as input size 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:
: Constant time (e.g., accessing an array element by index).
: Logarithmic time (e.g., Binary Search).
: Linear time (e.g., Linear Search, single loop).
: Linearithmic time (e.g., Merge Sort).
: Quadratic time (e.g., Bubble Sort, nested loops).
: Exponential time (e.g., recursive Fibonacci).
Searching and Sorting Algorithms
1. Searching Techniques
Linear Search:
Sequentially compares each element from index to . Operates on unsorted data.
Complexity: Best case , Average case , Worst case , Space complexity .
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 and halving the search space.
Complexity: Best case , Average/Worst case , Space complexity .
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 . Best case , Average/Worst case , Space ; 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 , Space ; 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)
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 , Average/Worst case , Space ; 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)
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 , Space 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)
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 , Worst case , Space 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 ]
Heap Sort:
Builds a Max Heap in time, then repeatedly swaps the root with the last leaf, decrements heap size, and runs
heapify().Complexity: Best/Average/Worst case , Space ; 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 to achieve average search, insertion, and deletion.
Common Hash Function: Division method: , where is the table size.
Collisions and Overflow:
Collision: Occurs when two distinct keys yield an identical hash value ().
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:
Quadratic Probing:
Double Hashing:
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 ]
Linear Data Structures: Lists, Stacks, and Queues
1. Linked Lists
Singly Linked List:
Unidirectional chain of nodes containing
dataand anextpointer.Insertion/deletion at the head takes ; arbitrary position operations and searches require traversal ().
Diagram:
text head ---> [ 10 | • ] ---> [ 20 | • ] ---> [ 30 | NULL ]
Doubly Linked List (DLL):
Nodes contain
prev,data, andnextpointers. Enables bidirectional traversal and simplified deletion.Diagram:
text NULL <--- [ • | 10 | • ] <===> [ • | 20 | • ] <===> [ • | 30 | • ] ---> NULL
Circular Linked List:
The final node points back to the initial node rather than
NULL. Traversed viado-whileloops until the head is reached again; used for Round Robin CPU scheduling, turn-based games, and audio playlists.