Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

Python please

compare_sorts.py

import random #Import module for generating random numbers
import time #Import module for getting the current 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
# See Canvas assignment for details.
main()

Use the starter-code compare_sorts.py to write a main() function that compares the
speed of three sorting algorithms. The sorting algorithms are provided as three
functions. Do not edit the sorting functions. Only edit the main() function. Add
code to the main() function as follows:
• The starter-code imports two modules: random and time. As you know, the
random module has functions for generating random numbers. The time module
provides the function time.time() which returns the current time.
• Use the predefined global variable maxvalue to create 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 merge.sort() 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 merge_sort() to sort the list.
• Generate a new random list using the same maxvalue and measure and print the
time for the insertion_sort to sort the list.
• Generate a new random list using the same maxvalue and measure and print the
time for the bubble_sort to sort the list.
run the program several times with these
different values of maxvalue: 100, 1000, 10000, and 100000. For each value of
maxvalue, write down the time required for each sorting algorithm, then using a text
editor, hand-type the times into a results.txt file.
Hint: Some of the sorting algorithms take a really long time to sort 100000 numbers.
Be patient. Get a snack or something while you wait for them to finish.
expand button
Transcribed Image Text:Use the starter-code compare_sorts.py to write a main() function that compares the speed of three sorting algorithms. The sorting algorithms are provided as three functions. Do not edit the sorting functions. Only edit the main() function. Add code to the main() function as follows: • The starter-code imports two modules: random and time. As you know, the random module has functions for generating random numbers. The time module provides the function time.time() which returns the current time. • Use the predefined global variable maxvalue to create 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 merge.sort() 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 merge_sort() to sort the list. • Generate a new random list using the same maxvalue and measure and print the time for the insertion_sort to sort the list. • Generate a new random list using the same maxvalue and measure and print the time for the bubble_sort to sort the list. run the program several times with these different values of maxvalue: 100, 1000, 10000, and 100000. For each value of maxvalue, write down the time required for each sorting algorithm, then using a text editor, hand-type the times into a results.txt file. Hint: Some of the sorting algorithms take a really long time to sort 100000 numbers. Be patient. Get a snack or something while you wait for them to finish.
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education