Dsa Swayam
Dsa Swayam
- Time Complexity: Merge Sort offers a stable O(n log n) time complexity in all cases (worst,
best, and average), which is critical when handling large datasets like 100 million entries.
- Space Complexity: Merge Sort has a space complexity of O(n) because it requires auxiliary
space for merging. Although higher than Quick Sort, its consistent performance justifies the
trade-off.
- Stability and Practical Constraints: Merge Sort is stable and ideal for sorting linked lists or
large datasets stored in external memory.
Quick Sort, while faster on average, has a worst-case time complexity of O(n^2) and is less
predictable. Bubble Sort and Selection Sort are inefficient (O(n^2)) and unsuitable for large-
scale data.
Conclusion: Merge Sort is the best choice for sorting massive datasets efficiently and
reliably.
1. Divide the data into manageable chunks that fit into RAM.
2. Sort each chunk using an in-memory sorting algorithm (like Merge Sort).
3. Store the sorted chunks on the disk.
4. Use a k-way merging algorithm to combine the sorted chunks into a single sorted dataset.
Why Preferred?
Multi-way Merge Sort is ideal for external sorting due to its efficiency in merging sorted
chunks without requiring the entire dataset in memory.
B-Tree Operations:
B+ Tree Operations:
- Similar to B-Tree but only leaf nodes store data, while internal nodes store keys.
- Leaf nodes are linked for fast sequential access.
Efficiency Comparison:
- **B+ Trees** are more efficient for range queries and large datasets due to sequential
linking of leaf nodes.
- Searching is faster in B+ Trees since all data is in the leaves.
Example: Consider inserting keys [10, 20, 5, 6, 12, 30, 7] and deleting 6. B+ Tree maintains
sorted leaf level links, aiding in efficient retrieval.
Conclusion: B+ Tree is preferred for efficient indexing in databases with large datasets.
Example:
Cities: A, B, C, D, E
Connections:
- A-B: 2
- A-C: 3
- B-D: 4
- C-D: 1
Result: The cities are connected with the minimum total cost without forming cycles.