CSE221
Algorithms: Linear Search
Summer 2020
Linear Search
• The most basic searching algorithm
• Given an item to search from a bucket of items, we search ONE BY
ONE.
• If we find then bingo! Else we look till the end of bucket.
• Example: Given an array of items. We will try to find “xyz” from it
using linear search.
0 1 2 3
cxc dfd dfd dfd
Summer 2020
Linear Search
Simulation (Iterative and Recursive)
• As mentioned in the previous slide linear search is done one by one
hence we start searching for “xyz” from index 0. If the items match
we stop else we look for the next item in the array.
0 1 2 3
cxc dfd dfd dfd
xyz xyz xyz xyz
Summer 2020
Linear Search
Simulation (Iterative and Recursive)
0 1 2 3
cxc dfd xyz dfd
xyz xyz xyz
Summer 2020
Linear Search Starting from index 0 of the array A
Pseudo code (Iterative)
Loop to traverse till the end of the array
boolean linearSearch(A[], item){
Checking if the ith element of the
int i = 0; array matches with item
while(i < A.length){ If matches, then return true, not
if (A[i] matches item){ searching further.
return true; If mismatch in the if condition,
} this line will run moving to the
i++; next index of the array.
} The program will reach this line after while
return false; loop is complete. This means all indices are
traversed and item could not be found.
}
False is returned.
Summer 2020
Linear Search
Time Complexity (Iterative)
boolean linearSearch(A[], item){ Was 0 1 2 3
looking
int i = 0; for xyz cxc dfd dfd dfd
while(i < A.length){
if (A[i] matches item){
return true; The worst case scenario occurs when the loop runs from
} index 0 to the end of the array. That is the item is found in
i++; the last index or not found at all. Recall the above scenario.
} If there are n indices in an array, the worst case would be O(n)
return false;
} Now for the best case to occur the item we are searching for
appears at the beginning as a result the number of search is
just 1. This is an once is a blue moon situation must be ignored.
Summer 2020
Linear Search
Pseudo code (Recursive)
boolean linSearch(A[], i, key){ Note that the CORE of the algorithm is the same.
if (i>=A.length){ Using recursion we replacing the loop with method
return false; call.
}else{
if (A[i]==key){
return true;
}else{
i++;
return linSearch(A, I, key);
}
}
}
Summer 2020
Linear Search
Recursion Tracing
Tracing recursive algorithms are not easy like the
iterative ones. Now how to know if a recursive
algorithm is correct? Simple, look at the recurrence
equation [the logic of the program]. The logic of linear
search is, searching starts from index 0 and proceed by
one index until you find the element or you reach the
end of the array.
You will not get it right on the first day. Practice and
patience. Recursion is fun!!
Summer 2020
Let A = 5 6 7 8
Linear Search i = 0 and key = 7. linSearch() method invoked with
parameters
Recursion Tracing
Every method call is saved in stack, with current values
boolean linSearch(A[], i, key){ Step no. Variable Scope
if (i>=A.length){ Values
1. return false; (Stack)
}else{
if (A[i]==key){ 1 A unchanged Scope 3
i=0 i becomes 1
2. return true;
key = 7 recursive call
}else{
i++; 2 i =1 Scope 3
3. return linSearch(A, i, key); i becomes 2
} recursive call
} 3 i=2 Scope 2
} return true
Summer 2020
Linear Search
Recursion Simulation
Refer to slide number 3 and 4.
Summer 2020