In: Computer Science
def sequentialSearch(theList, item):
#**** to be changed:
# counts the iterations of the while loop and
returns this value
found = False
index = 0
while index < len(theList) and not
found:
if theList[index] ==
item:
found = True
index = index +
1
return
found
def binarySearch(theList, item):
#**** to be changed:
# counts the iterations of the while loop and
returns this value
startIndex = 0
endIndex = len(theList)- 1
found = False
while startIndex <= endIndex and not
found:
middleIndex =
(startIndex + endIndex)//2
if item ==
theList[middleIndex]:
found = True
elif item >
theList[middleIndex]:
startIndex = middleIndex + 1
else:
endIndex = middleIndex - 1
return found
# program for testing the search algorithms
dataSet = [-200, -190, -180, -170, -160, -110, -105, -74, -30, -17,
-12, -1, 4, 5, 13, 26, 37, 42, 62, 96, 100, 110, 120,130]
print("\nSuccessful sequential search - all elements of the data
set")
totalComparisons = 0
#**** to be completed:
# list traversal on the dataSet list:
# for each item: perform a sequential
search and
#
display the number of comparisons,
# count the total number of
comparisons
print("Average number of comparisons:
"+str(totalComparisons/len(dataSet)))
print("\nUn-successful sequential search")
target =
196
# target not in the dataSet
comparisons = 0 #**** to be changed: perform a sequential search
for target
print ("target " + str(target), "\t comparisons used " +
str(comparisons))
print("\nSuccessful binary search - all elements of the data
set")
totalComparisons = 0
#**** to be completed:
# list traversal on the dataSet list:
# for each item: perform a binary search
and
#
display the number of comparisons,
# count the total number of
comparisons
print("Average number of comparisons:
"+str(totalComparisons/len(dataSet)))
print("\nUn-successful binary search")
target =
196
# target not in the dataSet
comparisons = 0 #**** to be changed: perform a binary search for
target
print ("target " + str(target), "\t comparisons used " +
str(comparisons))
make some change to the python according to the comment line and complete the following question.
Sequential Search Algorithm |
Binary Search Algorithm |
||
Successful searches |
Successful searches |
||
Average number of comparisons |
Average number of comparisons |
||
Maximum number of comparisons found at index _____________ |
Maximum number of comparisons found at index _____________ |
||
Minimum number of comparisons found at index____________ |
Minimum number of comparisons found at index____________ |
||
Un-Successful search |
Un-Successful search |
||
Number of comparisons |
Number of comparisons |
Sequential Search Algorithm |
Binary Search Algorithm |
||
Successful searches |
24 |
Successful searches |
24 |
Average number of comparisons |
12.5 |
Average number of comparisons |
3.9166666666666665 |
Maximum number of comparisons found at index (assuming index starts at 1) 24 |
24 |
Maximum number of comparisons found at index (assuming index starts at 1) 2,5,8,11,14,17,20,22,24 |
5 |
Minimum number of comparisons found at index______1______ |
1 |
Minimum number of comparisons found at index (assuming index starts at 1) 12 |
1 |
Un-Successful search |
1 |
Un-Successful search |
1 |
Number of comparisons |
24 |
Number of comparisons |
5 |
Below is the modified program to get the number of comparisons in each case as required.
def sequentialSearch(theList, item):
#**** to be changed:
# counts the iterations of the while loop and
returns this value
found = False
index = 0
compare_count=0
while index < len(theList) and not
found:
compare_count+=1
if theList[index] ==
item:
found = True
index = index + 1
return
compare_count
def binarySearch(theList, item):
#**** to be changed:
# counts the iterations of the while loop and
returns this value
startIndex = 0
endIndex = len(theList)- 1
found = False
compare_count=0
while startIndex <= endIndex and not
found:
middleIndex =
(startIndex + endIndex)//2
compare_count+=1
if item ==
theList[middleIndex]:
found = True
elif item >
theList[middleIndex]:
startIndex = middleIndex + 1
else:
endIndex = middleIndex - 1
return compare_count
# program for testing the search algorithms
dataSet = [-200, -190, -180, -170, -160, -110, -105, -74, -30, -17,
-12, -1, 4, 5, 13, 26, 37, 42, 62, 96, 100, 110, 120,130]
print("\nSuccessful sequential search - all elements of the data
set")
totalComparisons = 0
#**** to be completed:
# list traversal on the dataSet list:
# for each item: perform a sequential
search and
#
display the number of comparisons,
for num in dataSet:
comparisons =
sequentialSearch(dataSet,num)
print("target " + str(num), "\t comparisons used
" + str(comparisons))
# count the total number of
comparisons
totalComparisons= totalComparisons +
comparisons
print("Average number of comparisons:
"+str(totalComparisons/len(dataSet)))
print("\nUn-successful sequential search")
target =
196
# target not in the dataSet
comparisons = 0 #**** to be changed: perform a sequential search
for target
comparisons= sequentialSearch(dataSet, target)
print ("target " + str(target), "\t comparisons used " +
str(comparisons))
print("\nSuccessful binary search - all elements of the data set")
totalComparisons = 0
#**** to be completed:
# list traversal on the dataSet list:
# for each item: perform a binary search
and
#
display the number of comparisons,
for num in dataSet:
comparisons = binarySearch(dataSet,num)
print("target " + str(num), "\t comparisons used
" + str(comparisons))
# count the total number of
comparisons
totalComparisons= totalComparisons +
comparisons
print("Average number of comparisons:
"+str(totalComparisons/len(dataSet)))
print("\nUn-successful binary search")
target =
196
# target not in the dataSet
comparisons = 0 #**** to be changed: perform a binary search for
target
comparisons= binarySearch(dataSet, target)
print ("target " + str(target), "\t comparisons used " +
str(comparisons))
Please find below attached the schreeshot of the program and console output.