Consider an array has 10 elements and the searching element is at arra...
Concept:
Linear search:
A linear search, often known as a sequential search, is a technique for locating an element in a list. It systematically verifies each element of the list until a match is discovered or the entire list has been searched.
Algorithm:
linear_search(int a[], int n, int X)
{
for (int i = 0; i < n; i++)
{
if (a[i] == X)
return i+1;
}
}
Explanation:
Worst-case:
In the Linear Search Algorithm, the worst-case scenario happens when the item to be found is at the end of the Array or the element is not present in the array. Hence the linear search compares each element till the end. So it takes maximum time behavior in linear search.
Best case:
The best case in the Linear Search Algorithm happens when the item to be found is at the beginning of the Array.
Average case:
In the Linear Search Algorithm, the average scenario happens when the item to be found is somewhere in the center of the Array.
Explanation:
The given data,
Consider an array has 10 elements and the searching element is at array index 6. A starting element is present at index zero.
Example:
A[10]= {12, 5, 8, 56, 12, 7, 10, 15, 85, 105}
Searching element = 10
First searching element 10 compares with index 0 so not found and compares with the next index element. After It continues till index 6 and searching success.
Hence the total number of comparisons are= 7
Hence the correct answer is 7.