0% found this document useful (0 votes)
14 views20 pages

Linear and Binary Search

Uploaded by

Harshitha K
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)
14 views20 pages

Linear and Binary Search

Uploaded by

Harshitha K
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/ 20

GLOBAL INSTITUTE OF MANAGEMENT SCIENCES

DATA STRUCTURES

NAME:J.SHILPA

HARSHITHA.K
CLASS: I SEM MCA
TOPIC:LINEAR
SEARCH
BINARY
SEARCH
BUBBLE
SORT
INDEX
 INTRODUCTION
 COMPARISN
 LINEAR SEARCH ALGORITHM
 LINEAR SEARCH-PSEUDOCODE
 WORKING OF LINEAR SEARCH USING AN ARRAY
 BINARY SEARCH ALGORITHM
 BINARY SEARCH-PSEUDOCODE
 WORKING OF BINARY SEARCH USING AN ARRAY
 MENU DRIVEN PROGRAM FOR LINEAR SEARCH,BINARY SEARCH
AND BUBBLE SORT
INTRODUCTION

 ALGORITHM- It is a step by step procedure, that defines a set of


instructions to be executed in a certain order to get the desired output.
 LINEAR SEARCH- A search that finds an element in the list by searching the
element sequentially until the element is found in the list.
 BINARY SEARCH- A search that finds the middle element in the list
recursively until the middle element is matched with a searched element.
 BUBBLE SORT- Comparison-based algorithm in which each pair of adjacent
elements is compared and the elements are swapped if they are not in the
order.
COMPARISN
LINEAR SEARCH BINARY SEARCH
o Starts searching from the first o It divides the array into half by
element and scans one calculating array’s middle
element at a time without element.
jumping to the next element.
o It can be implemented on any o It can be implemented on two-
linear data structures. way traversal data structures.
o Less complex o Complex- Sorting required
o It uses iterative o It uses Divide and conquer
approach(sequential approach
approach)
o Suitable for large data
o Suitable for small data set set(time and speed efficient)
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.
LINEAR SEARCH-PSEUDOCODE
Step1: [initialization] Set pos=-1
Step2: [initialization] Set i=1
Step3: Repeat Step4 while i<=n
Step4: if a[i]= val
set pos= i
print pos
go to step 6
[end of if loop]
set i= i+1
[end of loop]
Step5: if pos =-1
print as value does not exist
[end of if loop]
Step6: exit
WORKING OF LINEAR SEARCH USING AN ARRAY
Now, let's see the working of the linear search Algorithm using an unsorted
array.
Let the elements of array are -

Let the element to be searched is K = 41


Now, start from the first element and compare K with each element of the
array.

The value of K,i.e, 41, is not matched with the first element of the array, so move to the
next element and follow the same process until the respective element is found.
Now, the element to be searched is found. So algorithm will return the index of
the element matched.
BINARY SEARCH ALGORITHM
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.
BINARY SEARCH-PSEUDOCODE
Step1: [initialization] set BEG= lower_bound, END= upper_bound, POS= -1
Step2: Repeat steps 3 and 4 while BEG<= END
Step3: Set MID= (BEG +END)/2
Step4: If a[MID]= val
Set POS= MID
Print POS
go to step 6
else if a[MID]>val
set END=MID-1 else
set BEG= MID+1
[end of if]
[end of loop]
Step5: If Pos=-1
print “value does not exist”
[end of if]
Step6: exit
WORKING OF BINARY SEARCH USING AN ARRAY
Let the elements of the sorted array are:

10 12 24 29 39 40 51 56 69

Let the element to search is, K = 56


We have to use the below formula to calculate the mid of the array :
mid = (beg + end)/2
So, in the given array -
beg = 0
end = 8
mid = (010 + 8)/2 12
= 4. So,24
4 is the29
mid of the
39 array40 51 56 69

A[mid]=39
A[mid]<k(or 39<56)
So, beg=mid+1=5, end=8
Now,mid=(beg+end)/2=13/2=6
10 12 24 29 39 40 51 56 69
A[mid]=51
A[mid]<k(or 51<56)
So, beg=mid+1=7, end=8
Now,mid=(beg+end)/2=15/2=7

10 12 24 29 39 40 51 56 69

A[mid]=51
A[mid]<k(or 51<56)
So, beg=mid+1=7,
end=8
Now,mid=(beg+end)/2=15/2=7

Now, the element to be searched is found. So algorithm will return the


index of the element matched.
MENU-DRIVEN PROGRAMME FOR LINEAR
SEARCH,BINARY SEARCH AND BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void main()
{
int ch,key, i, n, a[20],low,high,mid;
printf("\n enter the number of elements");
scanf("%d",&n);
printf("\n Enter the array elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array elements are:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n Enter the key element to be searched");
scanf("%d",&key);
while(1)
{
printf("\n 1---LINEAR SEARCH\t 2---BINARY SEARCH\t3—BUBBLE SORT\t4---EXIT");
printf("\n Enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1: LINEAR SEARCH
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\n %d is found at poition %d", key,i+1);
break;
}
}
if(i==n)
printf("\nKey element %d NOT FOUND",key);
break;
case 2:BINARY SEARCH
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{
if (a[mid] < key)
low = mid + 1;
else if (a[mid] == key)
{
printf("%d found at location %d.\n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if (low > high)
printf("Not found! %d isn't present in the list.\n", key);
break;
case3:BUBBLE SORT;
for(i=0;i<n;i==)
for(j=0;j<n-I;j++)
{
if(a[j]>a[j+1])
{
temp=a[j]
a[j]=a[j+1];
a[j]=temp;
}
}
printf(“The sorted array \n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
break;
case 4: exit(0);
default: printf("\nWrong Choice");
}
}
}
THANK YOU

BY: J.SHILPA

HARSHITHA.K

You might also like