Data Structures & Algorithms
DSA Part-1
- Introduction to Data Structures
- Asymptotic Notations: Big-O, Omega, and Theta Notations
- Time Complexity and Space Complexity Analysis of Algorithms
- 1D Arrays: Memory Representation and Address Calculation
- 2D Arrays: Row-Major and Column-Major Order Formulas and Numericals
- Array Operations: Traversal, Insertion, and Deletion with Algorithms
- Sparse Matrix: Triplet Representation and Linked Representation
- String Operations, Memory Storage, and Pattern Matching Basics
- Linear Search Algorithm and Complexity Analysis
- Binary Search Algorithm (Iterative and Recursive)
- Bubble Sort Algorithm and Optimization
- Selection Sort Algorithm and Step-by-Step Trace
- Insertion Sort Algorithm and In-Place Analysis
- Merge Sort Algorithm (Divide and Conquer Approach)
- Quick Sort Algorithm and Partitioning Methods
- Counting Sort Algorithm (Non-Comparison Sorting)
- Radix Sort Algorithm and Bucket Distribution
- Singly Linked List: Concept and Memory Allocation
- Singly Linked List: Insertion Operations
- Singly Linked List: Deletion Operations
- Singly Linked List: Reversal and Search Operations
- Doubly Linked List: Node Structure and Traversal
- Doubly Linked List: Insertion and Deletion Operations
- Circular Singly Linked List: Concept and Operations
- Circular Doubly Linked List: Concept and Operations
- Header Linked List: Grounded and Circular Header Lists
- Polynomial Representation and Addition using Linked List
- Stack Data Structure: Array Implementation
- Stack Implementation using Linked List
- Infix, Prefix, and Postfix Notations
- Infix to Postfix Conversion using Stack
- Infix to Prefix Conversion using Stack
- Evaluation of Postfix Expression using Stack
- Recursion and System Stack (Tower of Hanoi)
- Linear Queue: Array Implementation and Operations
- Drawbacks of Linear Queue and Memory Wastage
- Circular Queue: Array Implementation and Condition Checks
- Queue Implementation using Linked List
- Double-Ended Queue (Deque): Input and Output-Restricted
- Priority Queue: Concept, Array and Heap Representation
- Tree Data Structure: Terminology and Basic Concepts
- Binary Trees: Types and Properties
- Binary Tree Traversals: Inorder, Preorder, and Postorder
- Level Order Traversal of Binary Tree
- Binary Search Tree (BST): Concept and Search Operation
- Binary Search Tree (BST): Insertion and Deletion Operations
- AVL Tree: Concept and Balance Factor
- AVL Tree Rotations: LL, RR, LR, and RL Rotations
- Binary Heap: Min-Heap and Max-Heap Properties
- Heap Operations: Heapify, Insertion, and Deletion
- B-Tree: Properties, Structure, and Insertion
- B+ Tree: Structure and Comparison with B-Tree
- Graph Data Structure: Terminology and Types
- Graph Representation: Adjacency Matrix and Adjacency List
- Breadth First Search (BFS) Algorithm for Graphs
- Depth First Search (DFS) Algorithm for Graphs
- Hashing: Hash Tables and Hash Functions
- Collision Resolution: Open Addressing Techniques
- Collision Resolution: Separate Chaining Method
Time Complexity and Space Complexity Analysis of Algorithms
Kisi bhi algorithm ko design karte waqt do critical parameters measure kiye jaate hain: Time Complexity (algorithm kitna time leta hai execute hone me) aur Space Complexity (algorithm ko run karne ke liye kitni memory ki zaroorat padti hai). Ek ideal algorithm dono resources ka optimal use karta hai.
1. Time Complexity Analysis
Time complexity actual seconds ya milliseconds me measure nahi ki ja sakti kyunki processor speed, architecture, aur background operating system processes har system par alag hote hain. Isliye, time complexity ko number of basic operations (such as comparisons, additions, memory assignments) ke terms me input size n ke function ke roop me measure kiya jata hai.
Types of Time Complexity:
- Worst-case Time Complexity [W(n)]: Maximum time jo algorithm kisi bhi input size
nke liye le sakta hai. Yeh upper bound guarantee karta hai. - Best-case Time Complexity [B(n)]: Minimum time jo algorithm execute hone ke liye lega (e.g. Linear search me element pehli position par mil jaye toh
O(1)). - Average-case Time Complexity [A(n)]: Expected time over all possible valid inputs. Yeh calculation probability distribution par based hoti hai.
How to Calculate Time Complexity of Loops
Loops ki time complexity determine karne ke standard rules:
Case 1: Simple Linear Loop
// Loop runs exactly n times
for (int i = 0; i < n; i++) {
// Constant time operations O(1)
x = x + 1;
}
// Total Time Complexity = O(n)
Case 2: Nested Loops
// Outer loop runs n times, inner loop runs n times
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j]; // O(1)
}
}
// Total Time Complexity = n * n * O(1) = O(n^2)
Case 3: Logarithmic Loop (Multiplication/Division)
// Counter multiplies by 2 in each iteration
for (int i = 1; i <= n; i = i * 2) {
// Executes: 1, 2, 4, 8, ..., 2^k <= n
// k = log2(n)
}
// Total Time Complexity = O(log n)
2. Space Complexity Analysis
Space complexity ka matlab hota hai total memory amount jo algorithm ko execution ke dauran required hoti hai. Space complexity do parts se milkar banti hai:
Total Space = Fixed Space + Variable (Dynamic) Space
- Fixed Part: Instruction space (code bytes), simple variables (int, float), aur constants. Yeh input size par depend nahi karta.
- Variable Part: Dynamically allocated memory (malloc/calloc), reference variables, recursion stack space, aur input size
nke proportional arrays.
Auxiliary Space vs Space Complexity
Yeh distinction viva aur competitive exams me frequently pucha jata hai:
- Auxiliary Space: Algorithm dwara use kiya gaya extra ya temporary space, input data ko chhod kar. E.g., Merge sort me
O(n)auxiliary array lagti hai. - Space Complexity: Input data space + Auxiliary space ka sum.
+---------------------------+-----------------------------------+ | Algorithm / Operation | Time Complexity | Space Auxiliary | +---------------------------+-----------------------------------+ | Linear Search | O(n) | O(1) | | Binary Search (Iterative) | O(log n) | O(1) | | Binary Search (Recursive) | O(log n) | O(log n) stack | | Bubble / Selection Sort | O(n^2) | O(1) | | Merge Sort | O(n log n) | O(n) | | Quick Sort (Average) | O(n log n) | O(log n) stack | +---------------------------+-----------------------------------+
Agli post me hum start karenge Linear Data Structures me 1D Arrays: Memory Representation and Address Calculation.