2. Linear Search
in middle | | Worst Case | O(n) | Element at last position or not found | | Space Complexity | O(1) | No extra space needed |
Visualization:
Best Case (1 comparison):
[15, 25, 30, ...] → Found immediately!
Average Case (n/2 comparisons):
[10, 25, 30, 15, ...] → Found in middle
Worst Case (n comparisons):
[10, 25, 30, 40, 35, 15] → Found at end
or
[10, 25, 30, 40, 35, 20] → Not found (checked all)
2.4 Advantages and Disadvantages
Advantages:
- Simple to implement
- Works on unsorted arrays
- No preprocessing required
- Works well for small datasets
- No extra memory needed
Disadvantages:
- Slow for large datasets
- Inefficient compared to other algorithms
- Time complexity increases linearly
When to Use:
- Small datasets (n < 100)
- Unsorted data
- When simplicity is priority
- When data changes frequently