0% found this document useful (0 votes)
19 views

Searching

Uploaded by

bg834657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Searching

Uploaded by

bg834657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Searching

 Searching is the process of finding a given value position in a list of values.


 It decides whether a search key is present in the data or not.
 It is the algorithmic process of finding a particular item in a collection of items.
 It can be done on internal data structure or on external data structure.

Searching Techniques

To search an element in a given array, it can be done in following ways:


1. Sequential Search
2. Binary Search

Sequential Search
 Sequential search is also called as Linear Search.
 Sequential search starts at the beginning of the list and checks every element of the list.
 It is a basic and simple search algorithm.
 Sequential search compares the element with all the other elements given in the list. If
the element is matched, it returns the value index, else it returns -1.

The above figure shows how sequential search works. It searches an element or value
from an array till the desired element or value is not found. If we search the element
25, it will go step by step in a sequence order. It searches in a sequence order. Sequential
search is applied on the unsorted or unordered list when there are fewer elements in a
list.
Linear search is implemented using following steps...

 Step 1 - Read the search element from the user.


 Step 2 - Compare the search element with the first element in the list.
 Step 3 - If both are matched, then display "Given element is found!!!" and terminate
the function
 Step 4 - If both are not matched, then compare search element with the next element
in the list.
 Step 5 - Repeat steps 3 and 4 until search element is compared with last element in
the list.
 Step 6 - If last element in the list also doesn't match, then display "Element is not
found!!!" and terminate the function.

Example
Consider the following list of elements and the element to be searched...
Binary Search

 Binary Search is used for searching an element in a sorted array.


 It is a fast search algorithm with run-time complexity of O (log n).
 Binary search works on the principle of divide and conquer.
 This searching technique looks for a particular element by comparing the middle most
element of the collection.
 It is useful when there are large number of elements in an array.

 The above array is sorted in ascending order. As we know binary search is applied on
sorted lists only for fast searching.

Binary search is implemented using following steps...

 Step 1 - Read the search element from the user.


 Step 2 - Find the middle element in the sorted list.
 Step 3 - Compare the search element with the middle element in the sorted list.
 Step 4 - If both are matched, then display "Given element is found!!!" and terminate
the function.
 Step 5 - If both are not matched, then check whether the search element is smaller or
larger than the middle element.
 Step 6 - If the search element is smaller than middle element, repeat steps 2, 3, 4 and
5 for the left sublist of the middle element.
 Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4 and
5 for the right sublist of the middle element.
 Step 8 - Repeat the same process until we find the search element in the list or until
sublist contains only one element.
 Step 9 - If that element also doesn't match with the search element, then
display "Element is not found in the list!!!" and terminate the function.
Example

Consider the following list of elements and the element to be searched...


Binary searching starts with middle element. If the element is equal to the element that we
are searching then return true. If the element is less than then move to the right of the list
or if the element is greater than then move to the left of the list. Repeat this, till you find an
element.
Difference between Liner and Binary Search

You might also like