Answer:
Check the explanation
Step-by-step explanation:
Program:
from modules import binSearch
from modules import insertionSort
unsort_list=['Paul', 'Aaron', 'Jacob', 'James', 'Bill', 'Sara', 'Cathy', 'Barbara', 'Amy', 'Jill']
print("The list of unsorted items are given below")
for i in unsort_list:
print(i)
insertionSort(unsort_list)
print("The list of sorted items are given below")
for i in unsort_list:
print(i)
while True:
userInput=raw_input("Enter the search string or quit to exit.. ")
if userInput == "quit":
print("quitting...")
break
else:
if binSearch(unsort_list, 0, len(unsort_list), userInput) != False:
print("%s was found." %userInput)
else:
print("%s was not found" %userInput)
USER> cat modules.py
def insertionSort(listarray):
"""This api in this module sorts the list in insertion sort algorithm"""
length=len(listarray)
currentIndex =1
while currentIndex < length:
index=currentIndex
placeFoundFlag=False
while index > 0 and placeFoundFlag == False:
if listarray[index] < listarray[index-1]:
temp=listarray[index-1]
listarray[index-1]=listarray[index]
listarray[index]=temp
index=index-1
else:
placeFoundFlag=True
currentIndex=currentIndex+1
def binSearch (listarray, init, lisLen, searchString):
"""This api in thie module search the given string from a given list"""
#check the starting position for the list array
if lisLen >= init:
#find the center point to start the search
mid = init + int((lisLen - init)/2)
#check if the string matches with the mid point index
if listarray[mid] == searchString:
return listarray[mid]
#now we will search recursively the left half of the array
elif listarray[mid] > searchString :
return binSearch(listarray, init, mid-1, searchString)
#now we will search recursively the right half of the array
else:
return binSearch(listarray, mid+1, lisLen, searchString)
else:
#string now found, return False
return False
Output:
The list of unsorted items are given below
Paul
Aaron
Jacob
James
Bill
Sara
Cathy
Barbara
Amy
Jill
The list of sorted items are given below
Aaron
Amy
Barbara
Bill
Cathy
Jacob
James
Jill
Paul
Sara
Enter the search string or quit to exit.. Amy
Amy was found.
Enter the search string or quit to exit.. AAron
AAron was not found
Enter the search string or quit to exit.. James
James was found.
Enter the search string or quit to exit.. Irvinf
Irvinf was not found
Enter the search string or quit to exit.. quit
quitting...
Kindly check the code output below.