Searching on an array/list is to find a given element on the array and return whether it is found or not and return its position if found. Linear search and binary search are two popular searching algorithms on arrays.
Algorithm
An algorithmic program is a list of rules to follow in order to solve an issue. It is ordinarily used for processing, calculation and alternative connected computer and mathematical operations.
The characteristics of a good algorithm.
Liner Search
Linear search is a very basic and simple search algorithm. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found. Pseudocode for Liner Search
Read size,array[size], search from user
i=0
While i<size
IF
search==array[i]
write i
break;
Else
i++
Endif
Endwhile
Binary search
Binary search is the most popular Search algorithm. It is efficient and also one of the most commonly used techniques that is used to solve problems.
Pseudo code for Binary Search
Procedure binary search
a← sorted array
b← size of array
c← value to be searched
Set lowerBound = 1
Set upperBound = b
while c not found
if upperBound < lowerBound
EXIT: c does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if a[midPoint] < c
set lowerBound = midPoint + 1
if a[midPoint] > c
set upperBound = midPoint - 1
if a[midPoint] = c
EXIT: c found at location midPoint
end while
end procedure