Top 10 Advance Java Interview questions? What are the differences between abstract classes and interfaces in Java? What is the difference between ArrayList and LinkedList in Java? What is the purpose of the finalize() method in Java? What is polymorphism in Java and how is it achieved? What are the different types of inner classes in Java? What is the difference between static and non-static methods in Java? What are the different types of exceptions in Java and how do they differ? What is the difference between checked and unchecked exceptions in Java? How does Java handle multithreading and synchronization? What are the different types of JDBC drivers in Java and how do they differ?
Linear Search
Linear search is a very simple search algorithm. In this type of search, a sequential search is made over all items one by one. Every item is checked and if a match is found then that particular item is returned, otherwise the search continues till the end of the data collection.
Linear Search Program
def linear_search(list,target):
for i in range(0,len(list)):
if list[i] == target:
return i
return None
def verify(index):
if index is not None:
print("Target found at index: ",index)
else:
print("Target not found in index")
numbers = [1,2,3,4,5,6,7,8,9,10]
result=linear_search(numbers,7)
verify(result)
Output:-
Target found at index: 6
Comments
Post a Comment