Question
CSC 110 - Homework 6 - Analysis of Algorithms You will be writing two programs using the following data file that stores the populations of
CSC 110 - Homework 6 - Analysis of Algorithms
You will be writing two programs using the following data file that stores the populations of the 50 states (and DC) as of the 2010 census.
We learned this week, that a linear search (also known as sequential search) is not as efficient as a binary search when we are searching a sorted list. Write a python program that implements two different search algorithms: linear search and binary search.
Write the necessary functions to get the data from the data file and store it into two lists: stateList and popList.
Write a function that performs a linear search on the state data to find the population of a given state. Recall that in HW 5 you used a linear search to find the location of the olympic games in a given year. You can use this as a guide to write the function for this program. Your function should have the following definition:
def getPopLinear(stateList, popList, state):
# Fill in the code to find the state
# in the state list and return the
# associated population from the population list
# using Binary Search
return population
Write a function that performs a binary search on the state data to find the population of a given state. We solved a similar problem in the lab and you can find the binary search code on the Sample Code page. You can start with the following skeleton:
def getPopBinary(stateList, popList, state):
# Fill in the code to find the state
# in the state list and return the
# associated population from the population list
# using Binary Search
return population
Insert a counter in the code of each of the two getPop functions to count the important comparison instructions. At the end of the function, print the number of comparisons made to do the search.
Write a main function of the code so that it calls BOTH findLocation functions.
In the comments of the program, include a discussion about the worst case input for each of the two algorithms along with a description of a comparison of the performance of the two algorithms. When does linear search perform better than binary search?
Suppose you want to sort the state population data by population instead of state name. Write a function that sorts two lists based on the values of the first list in the parameters. Your function definition would be:
def dualSort(list1, list2):
The function will use the selection sort algorithm to sort on the values of list1, keeping the corresponding values of list2 in the right place. So if you swap a value in list1, the value in list2 in the same position should also be swapped. You can use the selection sort algorithm found here:
Test your function by using the getData() function in the program your wrote for part 1 and creating a main function to call both functions. Your main function might look something like this:
def main():
stateList, popList = getData()
popList, stateList = dualSort(popList, stateList)
for i in range(len(popList)):
print(stateList[i], popList[i])
(o & Home Canadians are in certain other man in a more OD ROAD (o & Home Canadians are in certain other man in a more OD ROAD
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started