The question is in bold the below information is just extra as a guide Use perf_count to generate a list of data, where the ith entry is the time tm(i) necessary for merge-sort to sort lists of randomly chosen elements of length 2 i for i = 1, . . . , 10. Using ideas from 8.1. Sorting experiments First we need to generate some shued lists to sort. The shuffle method from the random module is used to randomise a list. For example the code from random import shuffle mylist = list(range(1, n+1)) random.shuffle(mylist) will produce a randomly shuffled list of all the integers from 1 to n inclusive. The following code provides a way to generate list of different lengths (in this case of length 2i for different i) with the time it takes to sort each: listlengths = [] sorttimelist = [] for i in range(1, 5): listlengths.append(2**i) mylist = list(range(2**i)) shuffle(mylist) # here define sorttime (how long it takes to sort the list) sorttimelist.append(sorttime) To test how long a sorting algorithm takes to sort a large list of numbers we will import and use the perf_counter() function from the time module. This function returns the number of seconds since some reference time. For example, the code: time_start = time.perf_counter() selectionsort(mylist) time_end = time.perf_counter() sorttime = time_end - time_start will store the time needed for selection sort to sort mylist. You are only timing the sorting process so be careful not to include the time to create the unsorted list in your timing!
The question is in bold the below information is just extra as a guide
Use perf_count to generate a list of data, where the ith entry is the time tm(i) necessary for merge-sort to sort lists of randomly chosen elements of length 2 i for i = 1, . . . , 10. Using ideas from 8.1. Sorting experiments
First we need to generate some shued lists to sort. The shuffle method from the random module is used to randomise a list. For example the code
from random import shuffle
mylist = list(range(1, n+1))
random.shuffle(mylist)
will produce a randomly shuffled list of all the integers from 1 to n inclusive. The following code provides a way to generate list of different lengths (in this case of length 2i for different i) with the time it takes to sort each:
listlengths = []
sorttimelist = []
for i in range(1, 5):
listlengths.append(2**i)
mylist = list(range(2**i))
shuffle(mylist)
# here define sorttime (how long it takes to sort the list)
sorttimelist.append(sorttime)
To test how long a sorting
time_start = time.perf_counter()
selectionsort(mylist)
time_end = time.perf_counter()
sorttime = time_end - time_start
will store the time needed for selection sort to sort mylist. You are only timing the sorting process so be careful not to include the time to create the unsorted list in your timing!
Step by step
Solved in 4 steps with 5 images