Linear search is used on a collections of items. It relies on the technique of traversing a list from start to end by exploring properties of all the elements that are found on the way.
For example, consider an array of integers of size . You should find and print the position of all the elements with value . Here, the linear search is based on the idea of matching each element from the beginning of the list to the end of the list with the integer , and then printing the position of the element if the condition is `True'.
Implementation:
The pseudo code for this example is as follows :
for(start to end of array)
{
if (current_element equals to 5)
{
print (current_index);
}
}
For example, consider the following image:
If you want to determine the positions of the occurrence of the number in this array. To determine the positions, every element in the array from start to end, i.e., from index to index will be compared with number , to check which element matches the number .
Time Complexity:
The time complexity of the linear search is because each element in an array is compared only once.
Không có nhận xét nào:
Đăng nhận xét