For this project, you will import the **time** and **random** modules. You will also install the **matplotlib** package and import from it the **pyplot** module. You will also import the wraps() function from the functools module for use in your decorator. 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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

For this project, you will import the **time** and **random** modules. You will also install the **matplotlib** package and import from it the **pyplot** module. You will also import the wraps() function from the functools module for use in your decorator.

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.

def bubble_sort(a_list):    
  """    
  Sorts a_list in ascending order    
  """    
  for pass_num in range(len(a_list) - 1):        
    for index in range(len(a_list) - 1 - pass_num):            
      if a_list[index] > a_list[index + 1]:                
        temp = a_list[index]                
        a_list[index] = a_list[index + 1]                
        a_list[index + 1] = temp

 

def insertion_sort(a_list):    
  """    
  Sorts a_list in ascending order    
  """    
  for index in range(1, len(a_list)):        
    value = a_list[index]        
    pos = index - 1        
    while pos >= 0 and a_list[pos] > value:            
      a_list[pos + 1] = a_list[pos]            
      pos -= 1        
    a_list[pos + 1] = value

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. Below is an example of code to produce a graph comparing two series of data points - **you will need to modify it to graph your timing data**.
```
from matplotlib import pyplot
pyplot.plot([1, 2, 3, 4, 10], [1, 4, 9, 16, 100], 'ro--', linewidth=2, label='series 1')
pyplot.plot([1, 2, 3, 4, 10], [1, 3, 7, 20, 150], 'go--', linewidth=2, label='series 2')
pyplot.xlabel("the x label")
pyplot.ylabel("the y label")
pyplot.legend(loc='upper left')
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 is the same list for both curves you're plotting). 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. The label parameter assigns the label to be used for that line in the legend. The legend() function sets where on the graph the legend should be displayed. The xlabel() and ylabel() functions set the labels for the x and y axes. For your graph, the x axis is the number of elements being sorted, and the y axis is the time in seconds. **Your graph must include a legend and labels for the axes.**

include a line that calls your compare_sorts() function to generate the graph 


Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
File Input and Output Operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY