0
0
DSA Goprogramming~10 mins

Linear Search Algorithm in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Linear Search Algorithm
Start at first element
Check if element == target?
NoMove to next element
More elements?
NoTarget not found
Return index
Start from the first element, check each one against the target. If found, return index; else move to next until end.
Execution Sample
DSA Go
arr := []int{4, 2, 7, 1, 3}
key := 1
for i, v := range arr {
    if v == key {
        return i
    }
}
return -1
Search for key 1 in array by checking each element one by one.
Execution Table
StepOperationCurrent IndexCurrent ValueCheck (value == key?)ActionVisual State
1Start search---Start at index 0[4] -> [2] -> [7] -> [1] -> [3]
2Compare044 == 1? NoMove to next index[4] -> [2] -> [7] -> [1] -> [3]
3Compare122 == 1? NoMove to next index[4] -> [2] -> [7] -> [1] -> [3]
4Compare277 == 1? NoMove to next index[4] -> [2] -> [7] -> [1] -> [3]
5Compare311 == 1? YesReturn index 3[4] -> [2] -> [7] -> [1] -> [3]
6End---Search ends, index 3 returned[4] -> [2] -> [7] -> [1] -> [3]
💡 Target found at index 3, search stops.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
i (index)-01233
v (value)-42711
key111111
foundIndexN/AN/AN/AN/A33
Key Moments - 3 Insights
Why does the search stop immediately when the value matches the key?
Because linear search returns the index as soon as it finds the target, as shown in step 5 of the execution_table.
What happens if the target is not in the array?
The search checks all elements and then returns -1 after the last step, which is not shown here but would be after step 6.
Why do we check each element one by one instead of jumping around?
Linear search checks sequentially because it does not assume the array is sorted, so it must check every element until it finds the target or ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'v' at step 4?
A1
B7
C2
D4
💡 Hint
Check the 'Current Value' column at step 4 in the execution_table.
At which step does the search find the target value?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look for the step where 'Check (value == key?)' is 'Yes' in the execution_table.
If the key was 10 (not in array), what would happen to the 'Action' column in the last step?
AReturn index of last element
BStop at first element
CReturn -1 indicating not found
DReturn index 0
💡 Hint
Recall linear search returns -1 if target is not found after checking all elements.
Concept Snapshot
Linear Search Algorithm:
- Start at first element
- Check each element one by one
- If element == target, return index
- If end reached without match, return -1
- Works on unsorted arrays
- Simple but can be slow for large arrays
Full Transcript
Linear search starts at the first element of the array and compares each element to the target value. If a match is found, it immediately returns the index of that element. If no match is found after checking all elements, it returns -1 to indicate the target is not present. This method works on any array, sorted or not, but can be slow if the array is large because it checks every element one by one.