Linear Search
Linear search is an easy searching technique. It is also called as sequential search. The searching of key starts from the first element of an array in a sequence manner. If the key matches with the element in the array, it breaks the flow and returns 1.
Algorithm for Linear search
step 1:Read the key.
step 2:Start searching the key from left most of the array.
step 3:If the key matches return success.
step 4:else return -1.
Example:
Let us consider an array of elements 10, 50, 30, 70, 80, 60, 20, 90, 40. The key element is 20. The searching starts from first element i.e. 10.It didn’t matched , so it moves to next element and again checks. The key element is found at 6th position .It stops searching and returns success.
program on linear search in different languages
1.C
https://gist.github.com/sandy-2001/a26d28d6ae5bbe9f8e95f2935460d09b
2.Java
https://gist.github.com/sandy-2001/d4d484215464b52537cc2d68883ff6eb
3.Php
https://gist.github.com/sandy-2001/d6f26c6778f9a4dde2a5ef2911856d6f
Time complexity
Worst case performance O(n)
Best case performance O(1)
Average case performance O(n/2)
This is all about Linear search. In further upcoming stories I‘ll share different searching and sorting techniques.