NEED HELP PYTHON PROGRAMMING ONLY SORTING AND SEARCHES (CODE GIVEN AS WELL) Start with the code given to you below. This code will show you how to generate a list of 1000 unique random numbers between 1 and 32000. The list will not be in any specific order (i.e., not sorted). You only need to generate this list ONCE at the beginning of your code. You can work on this same list for all the following steps. 2. Linear Search a. Implement a simple linear search algorithm (check this week’s lecture if you do not remember what a linear search is) to look for a certain number. Use the code in the example below (the line that assigns a number to value) to determine what number you are trying to find. The code will guarantee that number is in your list. b. Make a loop to repeat the process of picking a search value (using the code below) and searching for it. Repeat the pick-a-number/linear-search 10 times. Time each iteration of your loop and calculate an average time for each search after it has been done ten times. Print the average time for linear searches. 3. Index Method a. Pick a new search value and use the list .index() method to locate the index of the value. b. Make another loop to repeat the process of picking a search value (using the code below) and searching for it. Repeat the pick-a-number/index()-search 10 times. Time each iteration of your loop and calculate an average time for each search after it has been done ten times. Print the average time for index() searches. 4. Binary Search a. Create the code to do a binary search algorithm. This code is a little tricker, so you can either try to do this on your own to challenge yourself or find some code on the Internet to do a binary search (there are a number of Python binary search code examples out there). For the binary search code, this is one time there is no problem copying a function directly from the internet directly. This does not mean that posting this entire assignment to a homework and programming site and getting the answer is OK, just getting the binary search code! b. If you create your own code for a binary search, you MUST put a comment in to indicate that you are not using anyone else’s code. If you are trying to do the code on your own, this will be considered when the assignment is graded. If you use code from the Internet (which is OK for the binary search code), you MUST put a comment in the code to indicate that you are using code from the Internet and where you obtained the code. c. Make another loop to repeat the process of picking a search value (using the code below) and searching for it. Repeat the pick-a-number/binary search 10 times. Time each iteration of your loop and calculate an average time for each search after it has been done ten times. Print the average time for binary searches. 5. Finally, put comments in your code to discuss the results you get. Things like: a. Which is the fastest? b. Which is the slowest? c. If you run your program multiple times, are the numbers consistent? CODE BELOW: ## ## Import libraries – This needs to be at the top of your code ## import random import time ## ## The next thing you need to do is to create a list that contains ## 1000 randomly generated numbers between 1 and 32,000. The numbers ## are in a random order (not sorted). ## data_set = random.sample(range(1, 32_001), k=1000) ## ## This bit of code will pick a random value from the list of numbers. ## Use this line of code to pick a search target EACH time before ## you start a new timed search. Please note that the search target ## is not an index into the data_set list, but one of the values ## contained in the data_set list. ## value = data_set[random.randint(1,1000)-1] ## ## Here is some example code to time something in Python. This just ## uses a simple for loop to add some numbers, but observe how the ## time commands are used to get a starting time before the loop ## and an ending time when it is done. These two times are subtracted ## to get the time it takes to run the code in between. ## start = time.time() num = 0 for x in range(1,10000): num += x end = time.time() print("Elapsed time",end-start,"seconds
NEED HELP PYTHON PROGRAMMING ONLY SORTING AND SEARCHES (CODE GIVEN AS WELL)
Start with the code given to you below. This code will show you how to generate a list
of 1000 unique random numbers between 1 and 32000. The list will not be in any
specific order (i.e., not sorted). You only need to generate this list ONCE at the
beginning of your code. You can work on this same list for all the following steps.
2. Linear Search
a. Implement a simple linear search
do not remember what a linear search is) to look for a certain number. Use the
code in the example below (the line that assigns a number to value) to
determine what number you are trying to find. The code will guarantee that
number is in your list.
b. Make a loop to repeat the process of picking a search value (using the code
below) and searching for it. Repeat the pick-a-number/linear-search 10 times.
Time each iteration of your loop and calculate an average time for each search
after it has been done ten times. Print the average time for linear searches.
3. Index Method
a. Pick a new search value and use the list .index() method to locate the index of
the value.
b. Make another loop to repeat the process of picking a search value (using the
code below) and searching for it. Repeat the pick-a-number/index()-search 10
times. Time each iteration of your loop and calculate an average time for each
search after it has been done ten times. Print the average time for index()
searches.
4. Binary Search
a. Create the code to do a binary search algorithm. This code is a little tricker, so
you can either try to do this on your own to challenge yourself or find some
code on the Internet to do a binary search (there are a number of Python
binary search code examples out there). For the binary search code, this is one
time there is no problem copying a function directly from the internet directly.
This does not mean that posting this entire assignment to a homework and
programming site and getting the answer is OK, just getting the binary search
code!
b. If you create your own code for a binary search, you MUST put a comment in
to indicate that you are not using anyone else’s code. If you are trying to do the
code on your own, this will be considered when the assignment is graded. If
you use code from the Internet (which is OK for the binary search code), you
MUST put a comment in the code to indicate that you are using code from the
Internet and where you obtained the code.
c. Make another loop to repeat the process of picking a search value (using the
code below) and searching for it. Repeat the pick-a-number/binary search 10
times. Time each iteration of your loop and calculate an average time for each
search after it has been done ten times. Print the average time for binary
searches.
5. Finally, put comments in your code to discuss the results you get. Things like:
a. Which is the fastest?
b. Which is the slowest?
c. If you run your program multiple times, are the numbers consistent?
CODE BELOW:
##
## Import libraries – This needs to be at the top of your code
##
import random
import time
##
## The next thing you need to do is to create a list that contains
## 1000 randomly generated numbers between 1 and 32,000. The numbers
## are in a random order (not sorted).
##
data_set = random.sample(range(1, 32_001), k=1000)
##
## This bit of code will pick a random value from the list of numbers.
## Use this line of code to pick a search target EACH time before
## you start a new timed search. Please note that the search target
## is not an index into the data_set list, but one of the values
## contained in the data_set list.
##
value = data_set[random.randint(1,1000)-1]
##
## Here is some example code to time something in Python. This just
## uses a simple for loop to add some numbers, but observe how the
## time commands are used to get a starting time before the loop
## and an ending time when it is done. These two times are subtracted
## to get the time it takes to run the code in between.
##
start = time.time()
num = 0
for x in range(1,10000):
num += x
end = time.time()
print("Elapsed time",end-start,"seconds")
Step by step
Solved in 3 steps with 2 images