Linear Binary Search
Linear Binary Search
Linear Search
What is Search?
Search is a process of finding a value in a list of values. In other words, searching is the process of locating
given value position in a list of values.
Linear search algorithm finds a given element in a list of elements with O(n) time complexity where n is total
number of elements in the list. This search process starts comparing search element with the first element
in the list. If both are matched then result is element found otherwise search element is compared with the
next element in the list. Repeat the same until search element is compared with the last element in the list,
if that last element also doesn't match, then the result is "Element not found in the list". That means, the
search element is compared with element by element in the list.
• Step 2 - Compare the search element with the first element in the list.
• Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function
• Step 4 - If both are not matched, then compare search element with the next element in the list.
• Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
• Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and
void main(){
int list[20],size,i,sElement;
Binary search algorithm finds a given element in a list of elements with O(log n) time complexity where n is
total number of elements in the list. The binary search algorithm can be used with only a sorted list of
elements. That means the binary search is used only with a list of elements that are already arranged in an
order. The binary search cannot be used for a list of elements arranged in random order. This search process
starts comparing the search element with the middle element in the list. If both are matched, then the result
is "element found". Otherwise, we check whether the search element is smaller or larger than the middle
element in the list. If the search element is smaller, then we repeat the same process for the left sublist of
the middle element. If the search element is larger, then we repeat the same process for the right sublist of
the middle element. We repeat this process until we find the search element in the list or until we left with a
sublist of only one element.
And if that element also doesn't match with the search element, then the result is "Element not found in the
list".
• Step 3 - Compare the search element with the middle element in the sorted list.
• Step 4 - If both are matched, then display "Given element is found!!!" and terminate the function.
• Step 5 - If both are not matched, then check whether the search element is smaller or larger than
• Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and 5 for the left
• Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and 5 for the right
• Step 8 - Repeat the same process until we find the search element in the list or until sublist contains
• Step 9 - If that element also doesn't match with the search element, then display
first = 0;
last = size - 1;
middle = (first+last)/2;