Searching
Searching
Algorithm
Presented by:
Malaika
oll number: 1882
Searching
Algorithms
Searching algorithms are essential tools in
computer science used to locate specific
items within a collection of data. These
algorithms are designed to efficiently
navigate through data structures to find the
desired information, making them
fundamental in various applications such
as databases, web search engines, and
more
These are types of Searching Algorithms
Search
Linear Binary
Search Search
Linear Search Algorithm
Linear search is a method for searching for an element in a
collection of elements. In linear search, each element of the
collection is visited one by one in a sequential fashion to find
the desired element. Linear search is also known
as sequential search.
Algorithm for Linear Search Algorithm:
The algorithm for linear search can be broken down into the
following steps:
•Start: Begin at the first element of the collection of
elements.
•Compare: Compare the current element with the desired
element.
•Found: If the current element is equal to the desired
element, return true or index to the current element.
•Move: Otherwise, move to the next element in the
collection.
•Repeat: Repeat steps 2-4 until we have reached the end of
collection.
•Not found: If the end of the collection is reached without
For example: Consider the array arr[] = {10, 50, 30, 70, 80, 20, 90,
40} and key = 30
Step 1: Start from the first element (index 0) and compare key with each element
(arr[i]).
•Comparing key with first element arr[0]. SInce not equal, the iterator moves to the
next element as a potential match.
•Comparing key with next element arr[1]. SInce not equal, the iterator
moves to the next element as a potential match.
Step 2: Now when comparing arr[2] with key, the value matches. So the Linear Search
Algorithm will yield a successful message and return the index of the element when key is
found (here 2).
#include<iostream.h>
#include<coin.h>
void main()
{
int am[5] (10, 20, 30, 40, 50), i, n, loc=-1;
Enter value to find 50
clrscr();
Value found at index 4
cout<" Enter value to find";
cin>>n;
for(i=0;i<5;i++)
loci
if(arr[i] == n)
if(loc == -1)
cout<<"value is not found in array":
else
cout<<"value is found at index";
getch();
Binary Search
Algorithm
Binary Search Algorithm is a
searching algorithm used in a sorted array
by repeatedly dividing the search
interval in half.
Binary Search Algorithm:
Below is the step-by-step algorithm for Binary Search:
•Divide the search space into two halves by
finding the middle index “mid”.
•Compare the middle element of the search space with
the key.
•If the key is found at middle element, the process is
terminated.
•If the key is not found at middle element, choose which half
will be used as the next search space.
• If the key is smaller than the middle element, then
the left side is used for next search.
• If the key is larger than the middle element, then
the right side is used for next search.
•This process is continued until the key is found or the total
How does Binary Search Algorithm work?
To understand the working of binary search,
consider the following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23,
38, 56, 72, 91}, and the target = 23.
How does Binary Search Algorithm work?
To understand the working of binary search,
consider the following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23,
38, 56, 72, 91}, and the target = 23.
How does Binary Search Algorithm work?
To understand the working of binary search,
consider the following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23,
38, 56, 72, 91}, and the target = 23.
How does Binary Search Algorithm work?
To understand the working of binary search,
consider the following illustration:
Consider an array arr[] = {2, 5, 8, 12, 16, 23,
38, 56, 72, 91}, and the target = 23.