3. What is searching?
• In computer science, searching is the process of finding an item with
specified properties from a collection of items.The items may be stored as
records in a database, simple data elements in arrays, text in files, nodes in
trees, vertices and edges in graphs, or maybe be elements in other search
place.
4. Why do we need searching?
• Searching is one of the core computer science algorithms. We know that
today’s computers store a lot of information.To retrieve this information
proficiently we need very efficient searching algorithms.
5. Types of searching
• Unordered linear/ sequential search
• Sorted/ Ordered linear/ sequential search
• Binary search
• Symbol tables and Hashing
• String searching algorithms
6. Unordered linear/ Sequential search
• Let us assume that given an array whose elements order is not known.That
means that the element of array are not sorted. In this case, to search for an
element we have to scan the complete array and see if the element is there
in given list or not.
7. Unordered linear/ Sequential search
int unorderedlinearsearch (int A[], int n, int data)
{
for (int i=0; i<n; i++)
{
if(A[i] == data)
return i;
}
return -1;
}
8. Search No. 405 from the given array
15 75 65405 551 1535 77 80
First of all compare the element to be searched with the index
element of array.
51 !=405 So move to next element.
15 !=405 Move to next element.
75 != 405 Move to next element.
35 !=405 move to next element.
405 ==405 SEARCH COMPLETE.
9. Binary Search
• Let us consider a problem of searching a word in a dictionary.Typically, we
directly go to some approximate page[say, middle page] start searching
from that point. If the name that we are searching is same then the search is
complete. If the page is before the selected pages then apply the process for
the first half otherwise apply the same process for the second half. Binary
search also works in the same way.The algorithm applying such strategy is
referred as binary search algorithm.
10. Search Number 75 from Array using binary
search
15 25 65
75 > 45
45 555 9535 75 85
According to
algorithm, first find
middle element of
your array.
MID= LOW+HIGH/2
Check if search
element and middle
element are same?
If yes your search is
over.
75 ≠ 45
If not equal then
Check if search
element is greater or
smaller than middle
value.
75 == 75
If search element is
greater than mid
element then make
mid + 1 low value and
find middle value
Search Complete
LOW MID HIGHMIDLOW