Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON:::: Use the time module to write a decorator function named sort_timer that times how many seconds it takes the decorated function to run. Since

PYTHON::::

Use the time module to write a decorator function named sort_timer that times how many seconds it takes the decorated function to run. Since sort functions don't need to return anything, have the decorator's wrapper function return that elapsed time.

To get the current time, call time.perf_counter(). Subtracting the begin time from the end time will give you the elapsed time in seconds.

Copy the code for bubble sort and insertion sort from Module 4 and decorate them with sort_timer.

Write a function called compare_sorts that takes the two decorated sort functions as parameters. It should randomly generate a list of 1000 numbers and then make a separate copy of that list, which you can do like this:

list_2 = list(list_1) 

Next it should time how long it takes bubble sort to sort one of those copies (using the decorated bubble sort), and then time how long it takes insertion sort to sort the other copy (using the decorated insertion sort). This gets us the first data point for each algorithm. The function should now repeat this for lists of size 2000, 3000, and so on, up to 10000. For each list size, the function should randomly generate a list of that size and time how long it takes each algorithm to sort it. The random numbers should all be integers in the range 1 <= r <= 10000. Once the function has the 10 time data points for each algorithm, it will generate a graph comparing them.

To generate random numbers, you will need to import the random module. The function call random.randint(a, b) returns a random integer N such that a <= N <= b. You should use a = 1 and b = 10000. It's fine if values appear in the list multiple times.

To generate a graph, you will need to install the matplotlib package and import pyplot from it. Here's an example of code to produce a graph comparing two series of data points:

from matplotlib import pyplot pyplot.plot([1, 2, 3, 4, 10], [1, 4, 9, 16, 100], 'ro--', linewidth=2) pyplot.plot([1, 2, 3, 4, 10], [1, 3, 7, 20, 150], 'go--', linewidth=2) pyplot.show() 

Each of the calls to the plot function plot a line. The call to the show function displays the graph. In the calls to the plot function, the first list is the list of x-coordinates (which for both lines on your graph will be the same array). The second list is the list of y-coordinates. The 'ro--' tells it to use red circles connected by a dashed line and 'go--' is the same except green instead of red. The linewidth parameter is self-explanatory.

Your file must be named: sort_timer.py

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2006 Edbt 2006 Workshops Phd Datax Iidb Iiha Icsnw Qlqp Pim Parma And Reactivity On The Web Munich Germany March 2006 Revised Selected Papers Lncs 4254

Authors: Torsten Grust ,Hagen Hopfner ,Arantza Illarramendi ,Stefan Jablonski ,Marco Mesiti ,Sascha Muller ,Paula-Lavinia Patranjan ,Kai-Uwe Sattler ,Myra Spiliopoulou ,Jef Wijsen

2006th Edition

3540467882, 978-3540467885

More Books

Students also viewed these Databases questions

Question

b. Where did they come from?

Answered: 1 week ago

Question

c. What were the reasons for their move? Did they come voluntarily?

Answered: 1 week ago

Question

5. How do economic situations affect intergroup relations?

Answered: 1 week ago