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

Linear Search and Binary Search

Uploaded by

shobigameenu
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
0% found this document useful (0 votes)
5 views

Linear Search and Binary Search

Uploaded by

shobigameenu
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/ 29

SRI RAMAKRISHNA ENGINEERING COLLEGE

[Educational Service : SNR Sons Charitable Trust]


[Autonomous Institution, Reaccredited by NAAC with ‘A+’ Grade]
[Approved by AICTE and Permanently Affiliated to Anna University, Chennai]
[ISO 9001:2015 Certified and all Eligible Programmes Accredited by NBA]
VATTAMALAIPALAYAM, N.G.G.O. COLONY POST, COIMBATORE – 641 022.

Department of Information Technology

20CS243-DATA STRUCTURES AND ALGORITHM ANALYSIS

Presentation by
Ms. R. Rajalakshmi
Assistant Professor / IT
COURSE OUTCOMES
CO1: Describe various data structures and algorithms for problem
solving
CO2: Choose appropriate data structures and algorithms for better
problem solving.
CO3: Solve computational problems using efficient algorithms and data
structures
CO4: Discriminate the performance of various algorithms and its
background data structures

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 2


Searching
Searching is a process of finding a particular element among several given elements.

The search is successful if the required element is found.

Otherwise, the search is unsuccessful.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 3


Searching Algorithms
Searching Algorithms are a family of algorithms used for the purpose of
searching.

The searching of an element in the given array may be carried out in the
following two ways

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 4


Linear Search
Linear Search is the simplest searching algorithm.

It traverses the array sequentially to locate the required element.

It searches for an element by comparing it with each element of the array one by one.

So, it is also called as Sequential Search.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 5


Linear Search
Linear Search Algorithm is applied when

No information is given about the array.

The given array is unsorted or the elements are unordered.

The list of data items is smaller.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 6


Linear Search 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.
20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 7
Linear Search
Search Element: 11

3 6 7 11 32 33 53
0 1 2 3 4 5 6
Step: 1
Search element (11) is compared with the first element(3)

0 1 2 3 4 5 6
3 6 7 11 32 33 53

11
Both are not matching. So move to next element.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 8


Linear Search
Step: 2
Search element (11) is compared with the next element(6)

0 1 2 3 4 5 6

3 6 7 11 32 33 53

11

Both are not matching. So move to next element.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 9


Linear Search
Step: 3
Search element (11) is compared with the next element(7)

0 1 2 3 4 5 6

3 6 7 11 32 33 53

11

Both are not matching. So move to next element.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 10


Linear Search
Step: 4
Search element (11) is compared with the next element(11)

0 1 2 3 4 5 6

3 6 7 11 32 33 53

11

Both are matching. So we stop comparing and display element found at index 3

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 11


Linear Search Implementation
#include<stdio.h> // Linear Search Logic
#include<conio.h> for(i = 0; i < size; i++)
{
void main(){ if(SElement == list[i])
int list[20],size,i,SElement; {
printf("Element is found at %d index", i);
printf("Enter size of the list: "); break;
scanf("%d",&size); }
}
printf("Enter any %d integer values: ",size); if(i == size)
for(i = 0; i < size; i++) printf("Given element is not found in the
list!!!");
scanf("%d",&list[i]); getch();
printf("Enter the element to be Search: "); }

scanf("%d",&SElement);

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 12


Binary Search
Binary search is a fast search algorithm.
This search algorithm works on the principle of divide and conquer.
Binary search can be implemented only on a sorted list of items. If the
elements are not sorted already, we need to sort them first.
Binary search is an efficient algorithm because it reduces the search
space by half with each iteration.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 13


How Binary Search works?
Start with a sorted collection of data, typically an array or a list.
Define two pointers, one at the beginning of the collection (left) and the other at the end (right).
Calculate the middle index of the current search space: middle = (left + right) / 2.
Compare the middle element to the target value you're searching for:
 If the middle element is equal to the target value, you've found it, and the search is successful.
 If the middle element is greater than the target value, adjust the right pointer to be one less
than the middle index (i.e., right = middle - 1), effectively eliminating the right half of the
search space.
 If the middle element is less than the target value, adjust the left pointer to be one more than
the middle index (i.e., left = middle + 1), effectively eliminating the left half of the search
space.
Repeat steps 3 and 4 until one of the following conditions is met:
 The target value is found (middle element equals the target).
 The left pointer crosses the right pointer, indicating that the target value is not in the
collection.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 14


Binary Search Algorithm
Recursive Method
Iteration Method

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 15


Binary Search Pseudocode
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 16
Binary Search Implementation
int binarySearch(int a[ ], int key, int beg, int end)
{
int mid; int main(void) {
if(beg>end)
return -1;
int array[] = {3, 4, 5, 6, 7, 8, 9};
mid = (beg + end)/2; int n ;
if(key== a[mid] ) int x = 4;
{ int result = binarySearch(array, x, 0, n - 1);
return mid; if (result == -1)
} printf("Not found");
else if(key < a[mid] ) else
{
printf("Element is found at index %d", result);
return binarySearch(a,key,beg,mid-1);
}
}
else
{
return binarySearch(a,key,mid+1,end);
}
}

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 17


Binary Search
Example: sorted array of integer keys.
Target=7

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 18


Binary Search
Example: sorted array of integer keys.
Target=7

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 19


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 20


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 21


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 22


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 23


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 24


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 25


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 26


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 27


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint.


Is target = midpoint key? YES.

20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 28


20CS243 - DATA STRUCTURES AND ALGORITHM ANALYSIS –MS. R. RAJALAKSHMI 29

You might also like