n python3!!! starter-code compare_sort_algorithms.py: import random import time maxvalue = 1000 def merge(left, right): result = [] left_idx, right_idx = 0, 0 while left_idx < len(left) and right_idx < len(right): # to change direction of sort, change direction of comparison if left[left_idx] <= right[right_idx]: result.append(left[left_idx]) left_idx += 1 else: result.append(right[right_idx]) right_idx += 1 if left: result.extend(left[left_idx:]) if right: result.extend(right[right_idx:]) return result def merge_sort(m): if len(m) <= 1: return m middle = len(m) // 2 left = m[:middle] right = m[middle:] left = merge_sort(left) right = merge_sort(right) return list(merge(left, right)) def insertion_sort(array): for slot in range(1, len(array)): value = array[slot] test_slot = slot - 1 while test_slot > -1 and array[test_slot] > value: array[test_slot + 1] = array[test_slot] test_slot = test_slot - 1 array[test_slot + 1] = value return array def bubble_sort(array): index = len(array) - 1 while index >= 0: for j in range(index): if array[j] > array[j + 1]: array[j], array[j + 1] = array[j + 1], array[j] index -= 1 return array def main(): #ADD CODE to compare the three list-sorting functions main()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

In python3!!!

starter-code compare_sort_algorithms.py

import random
import time

maxvalue = 1000

def merge(left, right):
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
# to change direction of sort, change direction of comparison
if left[left_idx] <= right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1
if left:
result.extend(left[left_idx:])
if right:
result.extend(right[right_idx:])
return result

def merge_sort(m):
if len(m) <= 1:
return m

middle = len(m) // 2
left = m[:middle]
right = m[middle:]

left = merge_sort(left)
right = merge_sort(right)
return list(merge(left, right))

def insertion_sort(array):
for slot in range(1, len(array)):
value = array[slot]
test_slot = slot - 1
while test_slot > -1 and array[test_slot] > value:
array[test_slot + 1] = array[test_slot]
test_slot = test_slot - 1
array[test_slot + 1] = value
return array

def bubble_sort(array):
index = len(array) - 1
while index >= 0:
for j in range(index):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
index -= 1
return array

def main():
#ADD CODE to compare the three list-sorting functions

main()

import random
import time
3
1
2
4
maxvalue
1000
6 - def merge(left, right):
7
result
left_idx, right_idx
while left_idx < len(left) and right_idx < len(right):
8
0, 0
9.
# to change direction of sort, change direction of comparison
if left[left_idx] <=
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1
10
11
right[right_idx]:
12
13
14 -
15
16
if left:
result.extend(left[left_idx:])
if right:
result.extend(right[right_idx:])
return result
17
18
19
20
21
22
def merge_sort(m):
if len(m)
23
24
<= 1:
25
return m
26
len(m) // 2
m[:middle]
m[middle:]
27
middle
28
left
29
right
30
merge_sort(left)
merge_sort(right)
return list(merge(left, right))
31
left
32
right
33
34
def insertion_sort(array):
for slot in range(1, len(array)):
value
35
36 -
37
array[slot]
38
test_slot
slot
1
while test_slot
array[test_slot + 1]
test_slot
-1 and array[test_slot] > value:
array[test_slot]
39
40
41
test_slot
- 1
42
array[test_slot + 1]
value
43
return array
44
45 - def bubble_sort(array):
46
index =
len(array)
1
47 -
while index >= 0:
48 -
49 -
for j in range(index):
if array[j] > array[j + 1]:
50
array[j], array[j + 1]
array[j + 1], array[j]
51
index
1
52
return array
53
54 - def main():
55
#ADD CODE to compare the three list-sorting functions
56 main()
Transcribed Image Text:import random import time 3 1 2 4 maxvalue 1000 6 - def merge(left, right): 7 result left_idx, right_idx while left_idx < len(left) and right_idx < len(right): 8 0, 0 9. # to change direction of sort, change direction of comparison if left[left_idx] <= result.append(left[left_idx]) left_idx += 1 else: result.append(right[right_idx]) right_idx += 1 10 11 right[right_idx]: 12 13 14 - 15 16 if left: result.extend(left[left_idx:]) if right: result.extend(right[right_idx:]) return result 17 18 19 20 21 22 def merge_sort(m): if len(m) 23 24 <= 1: 25 return m 26 len(m) // 2 m[:middle] m[middle:] 27 middle 28 left 29 right 30 merge_sort(left) merge_sort(right) return list(merge(left, right)) 31 left 32 right 33 34 def insertion_sort(array): for slot in range(1, len(array)): value 35 36 - 37 array[slot] 38 test_slot slot 1 while test_slot array[test_slot + 1] test_slot -1 and array[test_slot] > value: array[test_slot] 39 40 41 test_slot - 1 42 array[test_slot + 1] value 43 return array 44 45 - def bubble_sort(array): 46 index = len(array) 1 47 - while index >= 0: 48 - 49 - for j in range(index): if array[j] > array[j + 1]: 50 array[j], array[j + 1] array[j + 1], array[j] 51 index 1 52 return array 53 54 - def main(): 55 #ADD CODE to compare the three list-sorting functions 56 main()
Practice writing code to compare three different sorting algorithms.
Section 1.
Write a main() function that compares the speed of three sorting algorithms using the
starter-code compare_sort_algorithms.py. The sorting algorithms are provided as three
functions. You should not edit the sorting functions. You can only edit the main()
function. Write additional code to the main() function as follows:
Use the global variable maxvalue to generate a random list of maxvalue integers between
1 and maxvalue* , call time.time() to get the start time and save it in a variable, then call the
merge_sort() function to sort the list, then call time.time() again to get the end time. The
elapsed time will be the end time minus the start time. Print the elapsed time for the
merge_sort to sort the list.
• Create a new random list using the same maxvalue and similarly measure the time for
the insertion_sort to sort the list.
Create a new random list using the same maxvalue and similarly measure the time for
the bubble_sort to sort the list.
Section 2.
If you have completed Section 1, run the program multiple times using the following values
of maxvalue: 100, 1000, 10000, and 100000. For each value of maxvalue, write down the
time required for each sorting algorithm and enter it into the results.txt file by hand using a
text editor.
Transcribed Image Text:Practice writing code to compare three different sorting algorithms. Section 1. Write a main() function that compares the speed of three sorting algorithms using the starter-code compare_sort_algorithms.py. The sorting algorithms are provided as three functions. You should not edit the sorting functions. You can only edit the main() function. Write additional code to the main() function as follows: Use the global variable maxvalue to generate a random list of maxvalue integers between 1 and maxvalue* , call time.time() to get the start time and save it in a variable, then call the merge_sort() function to sort the list, then call time.time() again to get the end time. The elapsed time will be the end time minus the start time. Print the elapsed time for the merge_sort to sort the list. • Create a new random list using the same maxvalue and similarly measure the time for the insertion_sort to sort the list. Create a new random list using the same maxvalue and similarly measure the time for the bubble_sort to sort the list. Section 2. If you have completed Section 1, run the program multiple times using the following values of maxvalue: 100, 1000, 10000, and 100000. For each value of maxvalue, write down the time required for each sorting algorithm and enter it into the results.txt file by hand using a text editor.
Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Radix Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education