Question

In the PowerPoint slides for lecture 15, you will find a function that performs binary search...

In the PowerPoint slides for lecture 15, you will find a function that performs binary
search on a Python list using recursion. Modify that binarySearchRec(alist,
item) function so that it performs ternary search on a Python list. Your new function
should find two midpoints that divide the list into three approximately equal size
segments. If the item being searched for is found at either of the midpoints, the function
should return True. If the item being searched for is less than the value at the first
midpoint, ternary search continues with the segment to the “left” of the first midpoint. If
the item being searched for is greater than the value at the second midpoint, ternary
search continues with the segment to the “right” of the second midpoint. Otherwise,
ternary search continues with the segment between the first and second midpoints. Name
your function ternarySearchRec.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

def ternary_search (alist, item):
   # Initial values (List Boundaries)
   first = 0
   last = len(alist) - 1
   # Set found to false
   found = False
   # Iterate till first doesn't cross last and item not found
   while first <= last and not found:
       # First mid point
       mid1 = first + ((last - first) // 3)
       # Second mid point
       mid2 = first + (2 * ((last - first) // 3))
       # Comparing for item at starting
       if item == alist[first]:
           found = True
       # Comparing for item at ending
       elif item == alist[last]:
           found = False
       # Checking at edge values
       elif item < alist[first] or item > alist[last]:
           return False
       # Moving to first sub list
       elif item <= alist[mid1]:
           last = mid1
       # Moving to second sub list
       elif item > alist[mid1] and item <= alist[mid2]:
           first = mid1 + 1
           last = mid2
       else:
           first = mid2 + 1
   return found
  
  
def main():
   """ Main function """
   # List
   L = [1, 2, 3, 4, 5, 6, 7, 8]
  
   # Printing list
   print(L)
  
   # Checking function
   if ternary_search(L, 6):
       print("\nElement 6 found...\n")
   else:
       print("\nElement 6 not found...\n")
      
   if ternary_search(L, 22):
       print("\nElement 22 found...\n")
   else:
       print("\nElement 22 not found...\n")
  
  
# Calling main function
main()

_______________________________________________________________________________________________

Code Screenshot:

_______________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
In the PowerPoint slides for lecture 15, you will find a function that performs binary search...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT