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.
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:
In the PowerPoint slides for lecture 15, you will find a function that performs binary search...
The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...
Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...
Python Problem: Variance Explanation: Problem 1: Computing variance For this problem, you will write a function variance that takes a list whose elements are numbers (floats or ints), and returns their variance, a single number. (If you don't remember how to compute variance, check the lecture notebook; it's one of the intermediate steps in computing standard deviation.) You should not use NumPy or any other Python libraries for this problem. For now, worry only about correctness, not efficiency. Passing an...
In Python! Search Exercise 11.A: Mergesort with a Comparator CS 1410 Background The sort algorithms we have looked at in Module 8 have all sorted list elements in ascending because it compares elements with the less-than operator. For example, in our mergesort program, the comparison appears as follows: 18 L[1] R01 The effect of this comparison is that if L(i) is less than RC), then L[is considered to come before R[i] in the sorted result. Hence the ascending order of...
I need help writing this code in C++ Proj11.cpp is provided as well as the randomdata.txt thank you in advance! Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...
SELF-CHECK Why did the first version of method search that passed the first test itemNotFirstElementInSingleElementArray contain only the statement return −1? Assume the first JUnit test for the findLargest method described in Self-Check exercise 2 in section 3.4 is a test that determines whether the first item in a one element array is the largest. What would be the minimal code for a method findLargest that passed this test? PROGRAMMING Write the findLargest method described in self-check exercise 2 in...
C++ LAB 19 Construct functionality to create a simple ToDolist. Conceptually the ToDo list uses a structure called MyToDo to hold information about each todo item. The members of the MyToDo struct are description, due date, and priority. Each of these items will be stored in an array called ToDoList. The definition for the MyToDo struct should be placed in the header file called ToDo.h Visually think of the ToDoList like this: There are two ways to add items to...
You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...
Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...
What is the role of polymorphism? Question options: Polymorphism allows a programmer to manipulate objects that share a set of tasks, even though the tasks are executed in different ways. Polymorphism allows a programmer to use a subclass object in place of a superclass object. Polymorphism allows a subclass to override a superclass method by providing a completely new implementation. Polymorphism allows a subclass to extend a superclass method by performing the superclass task plus some additional work. Assume that...