Concept Flow - Binary Search vs Linear Search Real Cost Difference
Start Search
Is list sorted?
|Yes
Binary Search
Set low=0, high=n-1
Calculate mid=(low+high)/2
Compare target with mid element
Found, return index
high=mid-1
low=mid+1
Repeat until low > high
Not found, return -1
If list not sorted
Linear Search
Start from index 0
Compare each element with target
Found, return index
Move to next index
Repeat until end of list
Not found, return -1
The flow shows how binary search works on a sorted list by dividing the search space, while linear search checks each element one by one when the list is unsorted.