0
0
DSA Goprogramming~10 mins

Binary Search vs Linear Search Real Cost Difference in DSA Go - Visual Comparison

Choose your learning style9 modes available
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.
Execution Sample
DSA Go
func linearSearch(arr []int, target int) int {
    for i, v := range arr {
        if v == target {
            return i
        }
    }
    return -1
}

func binarySearch(arr []int, target int) int {
    low, high := 0, len(arr)-1
    for low <= high {
        mid := (low + high) / 2
        if arr[mid] == target {
            return mid
        } else if arr[mid] < target {
            low = mid + 1
        } else {
            high = mid - 1
        }
    }
    return -1
}
This code shows linear search checking each element and binary search dividing the list to find the target.
Execution Table
StepOperationSearch TypeIndex CheckedComparison ResultPointer ChangesVisual State
1Start linear searchLinear---[3, 5, 7, 9, 11]
2Check index 0Linear03 != 7-[3, 5, 7, 9, 11]
3Check index 1Linear15 != 7-[3, 5, 7, 9, 11]
4Check index 2Linear27 == 7-[3, 5, 7, 9, 11]
5Return index 2Linear---[3, 5, 7, 9, 11]
6Start binary searchBinary--low=0, high=4[3, 5, 7, 9, 11]
7Calculate midBinary2arr[2] == 7-[3, 5, 7, 9, 11]
8Return index 2Binary---[3, 5, 7, 9, 11]
9Start linear search for 12Linear---[3, 5, 7, 9, 11]
10Check index 0Linear03 != 12-[3, 5, 7, 9, 11]
11Check index 1Linear15 != 12-[3, 5, 7, 9, 11]
12Check index 2Linear27 != 12-[3, 5, 7, 9, 11]
13Check index 3Linear39 != 12-[3, 5, 7, 9, 11]
14Check index 4Linear411 != 12-[3, 5, 7, 9, 11]
15Return -1 (not found)Linear---[3, 5, 7, 9, 11]
16Start binary search for 12Binary--low=0, high=4[3, 5, 7, 9, 11]
17Calculate midBinary2arr[2] == 7 < 12low=3[3, 5, 7, 9, 11]
18Calculate midBinary3arr[3] == 9 < 12low=4[3, 5, 7, 9, 11]
19Calculate midBinary4arr[4] == 11 < 12low=5[3, 5, 7, 9, 11]
20low > high, return -1Binary---[3, 5, 7, 9, 11]
💡 Search ends when target is found or all elements checked (linear) or low > high (binary).
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 17After Step 18After Step 19Final
i (linear index)-012------
low (binary)----003455
high (binary)----444444
mid (binary)-----2234-
target77777712121212
found index--22-2----1
Key Moments - 3 Insights
Why does binary search require the list to be sorted?
Binary search divides the list based on comparisons; if the list is not sorted, the logic to discard half the list fails. This is shown in steps 6-20 where low and high pointers move based on sorted order.
Why does linear search check every element until it finds the target or reaches the end?
Linear search has no assumption about order, so it must check each element one by one as shown in steps 2-5 and 9-15 until it finds the target or finishes the list.
What happens when the target is not in the list in binary search?
Binary search moves low and high pointers until low > high, meaning the target is not found, as shown in steps 16-20 where low increments beyond high and returns -1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does binary search find the target 7?
AStep 18
BStep 4
CStep 7
DStep 15
💡 Hint
Check the 'Comparison Result' column for 'arr[2] == 7' in the binary search rows.
At which step does linear search conclude the target 12 is not found?
AStep 14
BStep 15
CStep 20
DStep 19
💡 Hint
Look for the linear search step with 'Return -1 (not found)' in the 'Operation' column.
If the array was unsorted, which search method would still guarantee finding the target?
ALinear Search
BBinary Search
CBoth
DNeither
💡 Hint
Refer to the concept flow where linear search works on unsorted lists but binary search requires sorted lists.
Concept Snapshot
Binary Search vs Linear Search

- Linear Search checks each element one by one.
- Binary Search divides sorted list to find target faster.
- Binary Search needs sorted data; Linear does not.
- Binary Search is O(log n), Linear is O(n).
- Use Binary Search for sorted arrays for efficiency.
Full Transcript
This lesson compares binary search and linear search by showing how each checks elements to find a target value. Linear search checks elements one by one from start to end, suitable for unsorted lists but slower for large data. Binary search works only on sorted lists by repeatedly dividing the search range, quickly narrowing down where the target could be. The execution table traces both searches finding the target 7 and failing to find 12, showing pointer movements and comparisons. Key moments clarify why binary search requires sorted data and how linear search always checks all elements. The visual quiz tests understanding of steps where targets are found or not found and which search works on unsorted data. The snapshot summarizes the main differences and when to use each search method.