Linear and Binary Search
Linear and Binary Search
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
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
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
BY: J.SHILPA
HARSHITHA.K