๐ DSA with C – Day 3: Bubble Sort & Binary Search
๐ฏ Goal:
-
Learn Bubble Sort and sort arrays.
-
Understand how Binary Search works.
-
Compare Linear vs Binary Search.
-
Practice problems on sorted arrays.
๐ง Theory
✅ Bubble Sort:
-
Repeatedly compares adjacent elements and swaps them if they’re in the wrong order.
-
Worst-case time complexity: O(n²)
-
Best-case (already sorted): O(n) with optimization
๐จ๐ป Bubble Sort in C
✅ Binary Search:
-
Efficient searching technique on sorted arrays.
-
Time Complexity: O(log n)
-
Repeatedly divide the array and compare with middle element.
๐จ๐ป Binary Search in C
๐ Linear vs Binary Search
Feature | Linear Search | Binary Search |
---|---|---|
Time Complexity | O(n) | O(log n) |
Sorted Array Required | ❌ No | ✅ Yes |
Speed | Slower | Faster for large data |
Approach | Scan all elements | Divide & Conquer |
๐งช Practice Problems:
-
Sort an array using Bubble Sort in descending order.
-
Count how many swaps were done during Bubble Sort.
-
Use Binary Search to find the number of occurrences of a given element.
-
If not found in Binary Search, return where it should be inserted.
๐ Homework for Day 3:
-
Implement recursive Binary Search.
-
Write a program that first sorts using Bubble Sort and then searches using Binary Search.
-
LeetCode problem suggestion: Search Insert Position
๐ Day 3 Summary
-
✅ Learned Bubble Sort (with optimization)
-
✅ Implemented Binary Search (iterative)
-
✅ Practiced comparing search algorithms
Comments
Post a Comment