Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The binarySearch definition that is given and is to be edited: def binarySearch(alist, item): first = 0 last = len(alist) - 1 found = False

image text in transcribedThe binarySearch definition that is given and is to be edited:

def binarySearch(alist, item):

first = 0

last = len(alist) - 1

found = False

while first

midpoint = (first + last)//2

if alist[midpoint] == item:

found = True

else:

if item

last = midpoint - 1

else:

first = midpoint + 1

return found

In the PowerPoint slides for lecture 15, you will find a function that performs binary search on a Python list using iteration. Modify that binarySearch (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 (hence the name "ternary"...look it up) 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 ternarySearch. 1. 95 95

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions