human
Write me a code to sentinel Linear Search and the other steps required given the following:
Sentinel Linear Search | Python3 implementation of the approach Function to search key in the given array.
Function to search x in the given array.
Last element of the array.
Element to be searched is placed at the last index.
Put the last element back.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to sentinel Linear Search and the other steps required.
 def sentinelSearch ( arr , n , key ) :
def sentinelSearch ( arr , n , key ) :
	last = arr [ n - 1 ]
	arr [ n - 1 ] = key
	i = 0
	while ( arr [ i ] != key ) :
		i += 1
	arr [ n - 1 ] = last
	if ( ( i < n - 1 ) or ( arr [ n - 1 ] == key ) ) :
		print ( key , " is ▁ present ▁ at ▁ index " , i )
	else :
		print ( " Element ▁ Not ▁ found " )
arr = [ 10 , 20 , 180 , 30 , 60 , 50 , 110 , 100 , 70 ]
n = len ( arr )
key = 180
sentinelSearch ( arr , n , key )