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

22CS302_LM8

The document discusses searching algorithms in arrays, focusing on Linear Search and Binary Search. Linear Search is a sequential method that checks each element until the desired one is found, while Binary Search operates on sorted arrays by repeatedly halving the search interval to improve efficiency. The document outlines the algorithms' steps, applications, advantages, and conditions for use.

Uploaded by

poojask1636
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

22CS302_LM8

The document discusses searching algorithms in arrays, focusing on Linear Search and Binary Search. Linear Search is a sequential method that checks each element until the desired one is found, while Binary Search operates on sorted arrays by repeatedly halving the search interval to improve efficiency. The document outlines the algorithms' steps, applications, advantages, and conditions for use.

Uploaded by

poojask1636
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

22XX302 - DATA STRUCTURES I

UNIT I & Searching over Arrays and Ordered Arrays

1. Searching in Array
Searching is one of the most common operations performed in an array.
Array searching can be defined as the operation of finding a particular element
or a group of elements in the array.

There are several searching algorithms. The most commonly used among them
are:

⮚ Linear Search
⮚ Binary Search
⮚ Ternary Search

1.1 Linear Search Algorithm

The linear search algorithm is defined as a sequential search algorithm


that starts at one end and goes through each element of a list until the desired
element is found; otherwise, the search continues till the end of the dataset. In
this article, we will learn about the basics of the linear search algorithm, its
applications, advantages, disadvantages, and more to provide a deep
understanding of linear search.
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 finding the
desired element, return that the desired element is not in the array.
How Does Linear Search Algorithm Work?
In Linear Search Algorithm, every element is considered as a potential match
for the key and checked for the same.
If any element is found equal to the key, the search is successful and the index
of that element is returned.
If no element is found equal to the key, the search yields “No match found”.
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).

1.2 Binary Search Algorithm (Ordered Array)


Binary Search Algorithm is a searching algorithm used in a sorted array by
repeatedly dividing the search interval in half. The idea of binary search is to
use the information that the array is sorted and reduce the time complexity to
O(log N).
Binary search is a search algorithm used to find the position of a target value
within a sorted array. It works by repeatedly dividing the search interval in half
until the target value is found or the interval is empty. The search interval is
halved by comparing the target element with the middle value of the search
space.
Conditions to apply Binary Search Algorithm in a Data
Structure:
To apply Binary Search algorithm:
● The data structure must be sorted.
● Access to any element of the data structure should take constant time.
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.
o If the key is smaller than the middle element, then the left side is used
for next search.
o 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 search space is
exhausted.

You might also like