0
0
DSA Typescriptprogramming~10 mins

Linear Search Algorithm in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Linear Search Algorithm
Start at index 0
Check if element at index == target?
NoMove to next index
Index < array length?
NoTarget not found
Return index
Start from the first element, check each one until the target is found or the list ends.
Execution Sample
DSA Typescript
function linearSearch(arr: number[], target: number): number {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1;
}
Searches the array from start to end to find the target value and returns its index or -1 if not found.
Execution Table
StepOperationIndex CheckedElement at IndexTargetMatch?ActionResult
1Check element043NoMove to next indexContinue searching
2Check element123NoMove to next indexContinue searching
3Check element233YesReturn indexFound at index 2
💡 Target found at index 2, search stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
i01222
arr[i]42333
target33333
resultundefinedundefinedundefined22
Key Moments - 3 Insights
Why does the search stop immediately when a match is found?
Because the algorithm returns the index as soon as arr[i] === target is true, as shown in step 3 of the execution_table.
What happens if the target is not in the array?
The loop completes without finding a match, and the function returns -1, indicating the target is not found (not shown here but implied after last index).
Why do we check each element one by one instead of jumping around?
Linear search checks elements sequentially because it does not assume the array is sorted, so it must check every element until it finds the target or reaches the end.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'i' when the target is found?
A0
B2
C1
D3
💡 Hint
Check the 'Index Checked' column in the row where 'Match?' is 'Yes'.
At which step does the algorithm decide to return the index?
AStep 3
BStep 2
CStep 1
DAfter the loop ends
💡 Hint
Look at the 'Action' column where it says 'Return index'.
If the target was 5 instead of 3, what would happen in the execution_table?
AThe search would find the target at index 1.
BThe search would stop at step 3 with a match.
CThe search would continue until the end and return -1.
DThe search would return index 0 immediately.
💡 Hint
Think about what happens when no element matches the target in the 'Match?' column.
Concept Snapshot
Linear Search Algorithm:
- Start at index 0
- Check each element sequentially
- 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 checks each element one by one to see if it matches the target value. If a match is found, it returns the index immediately. If the search reaches the end without finding the target, 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 may need to check every element.