Contents
Searching
A process of finding an element from list of element stored any order/randomly. Searching is of two types
- Linear search
- Binary search
- Linear search
Steps
- Each element of the array read one by one segmentally & compared with the desired element.
- Successful-element available
- Unsuccessful-all element is read and the desired element not found.
- n-no. of element array
What is linear Search?
Linear Search is also known as sequential search, elements of the array are searched sequentially.
AlgorithmRequirement->
n->no. of element in array.
Element-> element to be searched.
- Input an array a of n element and input element to be searched.
- Initialize i=0 and Repeat through step 3 if(i
- If (element== a[i]) , print “successful search”
- If i>=n) , print(“Unsuccessful search”)
Binary search
A technique that search an item by ordered set of item is known binary search
Search an item
Note: Elements are in ordered set
Efficient method
- find the middle element of the array
- Compare the middle element with data to be searched
desired element found successful Search
< search first half
> search second half
Working
Algorithm
- Input an array a and n items, the element is to be searched.
- Start=0 end=n-1 mid=(start+end)/2
- Repeat step 4 and 5 while (start<=end) and (a[mid!=element)
- If(element>a[mid])
start= mid+1
else
end= mid-1 - mid=(start+end)/2
- If a[mid]==element)
print “Successful Search” - If(start>end)
print “Unsuccessful Search”