Professional Documents
Culture Documents
Lect-08 Searchin
Lect-08 Searchin
Harish D. Gadade
www.harishgadade.com
Searching Techniques
● Linear Search
● Binary Search
2
Prof. Harish D. Gadade, COEP Technological University, Pune
Linear Search
● Linear search is a sequential search, which uses loops to step through
or to traverse an array or list.
● It compares each elements with the value being searched for, and stop
when either the value is found or the end of the array is encountered.
Example:
4
Prof. Harish D. Gadade, COEP Technological University, Pune
Linear Search
● Advantages:
● The linear search is simple-It is very easy to implement and easy to
understand.
● Disadvantages:
● It has very poor efficiency
● Worst Case: Target value is the last element of the array or list. So
the search will take amount of time proportional to the length of
array or list.
7
Prof. Harish D. Gadade, COEP Technological University, Pune
Linear Search
#Linear Search if count>=n:
list=[2,5,10,4,9,7,1] print("Element Not Found")
x=20 else:
count=0 print("Element found at",location+1)
n=len(list)
for i in range(0,len(list)):
if list[i]!=x:
count=count+1
continue
elif list[i]==x:
location=i
8
Prof. Harish D. Gadade, COEP Technological University, Pune
Binary Search
1. Requirements: List must be in sorted order.
a. Middle Element
10
Prof. Harish D. Gadade, COEP Technological University, Pune