100% found this document useful (1 vote)
241 views15 pages

DSA Searching Techniques.

This document discusses searching algorithms in data structures using C. It describes linear search and binary search. Linear search sequentially checks each element of an unsorted array to find a target value, with time complexity of O(n). Binary search works on a sorted array by dividing the array in half and recursively searching only one half, having time complexity of O(log n). Code examples and visualizations are provided for both searching techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
241 views15 pages

DSA Searching Techniques.

This document discusses searching algorithms in data structures using C. It describes linear search and binary search. Linear search sequentially checks each element of an unsorted array to find a target value, with time complexity of O(n). Binary search works on a sorted array by dividing the array in half and recursively searching only one half, having time complexity of O(log n). Code examples and visualizations are provided for both searching techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Government Polytechnic Lucknow

INFORMATION TECHNOLOGY 2ND


YEAR

Presented by : Krishnanand Mishra, Sumit Shukla , Ankur Kumar , Utkarsh Tiwari


Data structure using C

Searching

Linear Binary
search search
Searching

Searching in data-
structure refers to These set of items
Generally, Searching the process of are in various
means to find or finding a desired forms , such as an
discover something. element in set of Array, Tree, Graph,
items. or Linked-list .
Searching in Array

Searching in array If the desired value is


means to find present in the array, then If the desired value There are two
whether a searching is said to be is not present in popular methods for
particular value is successful. And the the array, then searching the array
present in array or searching process gives searching is said to elements.
not. the location of that value be unsuccessful.
in the array.

Linear Binary
search search
Linear Search
It is a very basic and simple search algorithm.

In linear search we search an element in given array by traversing the array from the
starting till the desired element is found.

Linear search is mostly used to search an unordered list of elements.

like : A[ ] = { 10,8,4,7,6,2,5,1,9};
Algorithm
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
Code
Int linear_search ( a[ ], LB, UB, item ) // LB= Lower bound, UB= upper bound,
{
int k;
for (k = LB ; k <= UB; K++)
{
if ( a [k] == item )
{
return k ;
}
}
return LB -1;
}
Binary search
Binary search is the search technique that works efficiently on sorted lists.

To search an element into some list using the binary search technique, we must ensure that the
list is sorted.

Binary search follows the divide and conquer approach in which the list is divided into two
halves, and the item is compared with the middle element of the list.

If the match is found then, the location of the middle element is returned. Otherwise, we
search into either of the halves depending upon the result produced through the match.
Algorithm
Step 1 - Start searching data from middle of the list.

Step 2 – If it is a match, return the index of the item, and exit.

Step 3 – If it is not a match, probe position.

Step 4 – Divide the list and find the new middle.

Step 5 – If data is greater than middle, search in higher sub-list.

Step 6 – If data is smaller than middle, search in lower sub-list.

Step 7 – Repeat until match


Example

.
Code
Int binary_ search ( A[ ], LB, UB, item ) // LB= Lower bound, UB= Upper bound,
{ int low=LB, high=UB;
int mid=(low + high) / 2;
while( (A[mid]!= item) && ( low<=high ) )
{ if( item<A[mid] )
high = mid -1 ;
else
low = mid +1 ;
mid = (low + high ) / 2;
}
if( A[ mid ]= item )
return min ;
else
return LB -1 ;
}
Linear search vs Binary search
Linear search Binary search
1. Data can be in any order. 1. Data should be in sorted order.
2. Multi dimensional array also can 2. Only single dimensional array is
be used. used.
3. Time complexity O(n). 3. Time complexity O ( log (n) ).
4. Not an efficient method to be used 4. Efficient for large inputs also.
if there is a large item.
Visualization

,
Thank you
Thank you

You might also like