Algorithms and Flowchart linear search
Linear Search
Q) Explain the linear search with an algorithm with an example.
Linear search is the sequential search. it is started with elements, in this search elements are checked sequentially until the required element is found.
When an element is found, the search is said to be successful. But when all elements are compared and the required element is not found then the search is said to be unsuccessful.

Algorithm: linear_search (Data[], N Item, loc)
- This is the algorithm for linear search
- Data[] โ Array of elements,
- item โ Element to be searched,
- N โ the size of an array
- loc โ location
- i โ index variable
step 1: Start
step 2: Set i = 1
step 3: Set loc = 0
step 4: Repeat steps 5 and 6 until i <= n and loc = 0
step 5: [compare Data[i] and item]
ย ย ย ย if data[i] == item the
ย ย ย ย ย set loc=i
ย ย ย ย ย Goto step7
ย ย ย ย end if
step 6: set i = i + 1
step 7: if loc == 0 then
ย ย ย ย ย print "item is not found"
ย ย ย Else
ย ย ย ย print "item is found at loc"
step 8: stop
Example Linear Search
Consider the following example, having 7array elements.
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
The element to search is: 64
Pass 1
Compare Data[1] with 64
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
As Data[1] != 64 go for next pass
ย
Pass 2
Compare Data[2] with 64
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
As Data[2] != 64 go for next pass
Pass 3
Compare Data[3] with 64
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
As Data[3] != 64 go for next pass
ย
Pass 4
Compare Data[4] with 64
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
As Data[4] != 64 go for next pass
ย
Pass 5
Compare Data[5] with 64
1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|
11 | 3 | 9 | 5 | 64 | 32 | 8 |
As Data[5] == 64 is Print โElement is Foundโ
Result: Given element 64 is found is the 5th position.