by Spas Arnaudov
Linear search is very simple algorithm. It uses "brute force" to find an element.
1. declare the array a n
2. declare "value"
3. User initialize "value"
4. declare "i" and initialized to 0
5. checking whether "i" is less than "n"
6. We check whether a i is equal to value
7. If not, keep looking
8. increment "i"
9. Repeat 6, 7 and 8 until we find a match
10. Print the index on which it stands element or 1 if no match
We iterate through all the array elements. We do check if the array element is equal to the target, interrupt the loop and indicate the position where the item is found. If you do not find such an element leave loop normally. Then we can not return index. It is customary to return 1.
This search method is very simple, but very effective. It is possible the item to be first or last. At best, we will have one iteration in the worst n number of iterations.
int linearSearch(int array[], int size, int searchElement)
{
int counter = 0;
int i;
for (i = 0; i < size; i++)
{
printf("Count of itarations is %d\n", ++counter);
if (array[i] == searchElement) // if the element is found
{
return i;
}
}
return 1;
}
Comments for Linear Search




Do you learn better from video?
Learn faster with deeper understanding! 
Did this help? Support me with your vote ;) 


Did this help? 

