Data Structures & Algorithms – Complete Notes
1. Introduction to Algorithms
Definition: An algorithm is a finite sequence of well-defined instructions to solve a problem.
Characteristics: Input, Output, Definiteness, Finiteness, Effectiveness.
Analysis: Measured by Space Complexity and Time Complexity.
Asymptotic Notations: O (upper bound), Ω (lower bound), Θ (tight bound).
2. Complexity of Algorithms
- Space Complexity = Fixed Part + Variable Part (recursion stack, dynamic variables).
- Time Complexity = Execution time measured in steps.
- Common Complexities: O(1), O(log n), O(n), O(n log n), O(n²).
3. Data Structures
Definition: A way of organizing data for efficient access and modification.
Abstract Data Type (ADT): Separates specification from implementation.
Types:
- Linear: Array, Linked List, Stack, Queue.
- Non-Linear: Tree, Graph.
4. Arrays
- Linear data structure storing same-type elements in contiguous memory.
- Operations: Traverse, Insert, Delete, Sort, Reverse.
- Associative Arrays: Key-value pairs (Python dict, C++ map, JS objects).
- Jagged Arrays: Array of arrays with different row lengths.
5. Searching Techniques
- Sequential Search: O(n), compares elements one by one.
- Sentinel Search: Variation of sequential, reduces comparisons.
- Binary Search: Works on sorted arrays, O(log n).
- Fibonacci Search: Uses Fibonacci numbers, O(log n).
6. Sorting
Need: Efficient searching, duplicate detection, data organization.
Types: Internal (Bubble, Insertion, Quick, Merge), External (K-way merge).
Stable Sort: Maintains relative order of equal keys.
Sorting Algorithms:
- Bubble Sort: O(n²), stable.
- Selection Sort: O(n²), not stable.
- Insertion Sort: O(n²), stable, good for small data.
- Quick Sort: Avg O(n log n), worst O(n²), not stable.
- Merge Sort: O(n log n), stable, requires extra space.
Comparison:
Bubble (O(n²), stable), Selection (O(n²), not stable), Insertion (O(n²), stable), Quick (O(n log n), not
stable), Merge (O(n log n), stable).
7. Hashing
- Provides O(1) average time for search, insert, delete.
- Types: Static Hashing, Dynamic Hashing.
- Hash Function: Maps key to address (Division, Mid-square, Folding, Digit analysis).
- Collision Handling:
• Open Addressing (Linear Probing).
• Chaining (linked list per bucket).
- Performance:
• Linear probing: Successful search ≈ (2 - α)/2α, where α = load factor.
• Chaining: Successful search = 1 + α/2.