Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Searching Techniques

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.

● If the value being searched is not found in array or list, the


algorithm will be “Unsuccessful Search” to the end of the array or
list.

● Since the list or array elements are stored in linear order,


searching the element in the linear order make it easy and efficient.

● The Search may be successful or Unsuccessful. 3


Prof. Harish D. Gadade, COEP Technological University, Pune
Linear Search

Example:

10, 20, 30, 40, 50 , 60

Searching Key, K=40

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.

● It does not require data in sorted order.

● Disadvantages:
● It has very poor efficiency

● Performance of algorithm scales linearly with size of input

● Linear search is slower than other algorithms.


5
Prof. Harish D. Gadade, COEP Technological University, Pune
Linear Search
● Analysis:
● Best Case: Target value is the first element of the list or array,
search can take very tiny and constant amount of time like single
comparison

● 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.

● Average case: Target value is somewhere in the array or list.The


target value may be anywhere in the list or array,any element of an
list or array is equally likely.
6
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

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.

2. Divide the sorted list in three sections

a. Middle Element

b. Elements on the left side of middle element.

c. Element on the right side of the elements.

3. If the Middle element is Searching key, then done.


4. If Searching key is smaller than Middle element, then process left of
middle element
5. If Searching key is greater than Middle Element, then process right of
the middle element. 9
Prof. Harish D. Gadade, COEP Technological University, Pune
Binary Search
#Binary Search while low <= high: if flag==1:
list = [ 2, 3, 4, 10, 40 ] mid = (high + low) // 2 print("Successful)
x = 50 if list[mid]==x: else:
low = 0 flag=1 print("Unsuccessful")
flag=0 break
high = len(list) - 1 elif list[mid] < x:
low = mid + 1
elif list[mid] > x:
high = mid - 1
else:
flag=0

10
Prof. Harish D. Gadade, COEP Technological University, Pune

You might also like