human
What is a code to ternary Search and the other steps required given the following:
Ternary Search | Function to perform Ternary Search.
Find mid1 and mid2.
Check if key is at any mid.
Since key is not present at mid , Check in which region it is present Then repeat the search operation in that region.
key lies between l and mid1.
key lies between mid2 and r.
key lies between mid1 and mid2.
key not found.
Get the list Sort the list if not sorted.
Starting index.
Length of list.
Key to be searched in the list.
Search the key using ternary search.
Print the result.
Key to be searched in the list.
Search the key using ternary search.
Print the result"
Solution in Python:
assistant
#Here is code to ternary Search and the other steps required.
 def ternarySearch ( l , r , key , ar ) :
	while r >= l :
		mid1 = l + ( r - l ) // 3
		mid2 = r - ( r - l ) // 3
		if key == ar [ mid1 ] :
			return mid1
		if key == mid2 :
			return mid2
		if key < ar [ mid1 ] :
			r = mid1 - 1
		elif key > ar [ mid2 ] :
			l = mid2 + 1
		else :
			l = mid1 + 1
			r = mid2 - 1
	return - 1
ar = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
l = 0
r = 9
key = 5
p = ternarySearch ( l , r , key , ar )
print ( " Index ▁ of " , key , " is " , p )
key = 50
p = ternarySearch ( l , r , key , ar )
print ( " Index ▁ of " , key , " is " , p )