Python Program to Create Timing Experiment Assignment Solution

July 03, 2024
Prof. Emily
Python
Prof. Emily Wong, with a Master's degree in Computer Science from the University of Sydney, has completed over 600 Python programming test assignments. She specializes in Python topics such as data visualization, web scraping, natural language processing, and game development.
Key Topics
• Instructions
• Requirements and Specifications
Tip of the day
News

Instructions

Objective
Write a python assignment program to create timing experiment.

Requirements and Specifications

Source Code

QUESTION 1

`import randomimport timedef bubble_sort(array):"""Given an array (list of integers), sort it using bubble sort"""n = len(array) # size of array# Loop through the elementsfor i in range(n):# Loop through the elements from 0 to n-i-1for j in range(n-i-1):if array[j] > array[j+1]: # the element on the left is higher than the element on the right# swaparray[j], array[j+1] = array[j+1], array[j]if __name__ == '__main__':# Ask user for size of arrayn = int(input("Enter size: "))# Create arrayarray = list()# Generate n random integers and fill arrayfor i in range(n):array.append(random.randint(-100,100)) # between -100 and 100# Print original arrayprint("Original Array", array)# Sortbubble_sort(array)# Print sorted arrayprint("Sorted Array:", array)input()`

QUESTION 2

`import randomimport timedef selection_sort(array):"""Given an array (list of integers), sort it using selection sort"""n = len(array) # size of array# Loop through the elementsfor i in range(n):# Loop through the elements from 0 to n-i-1min_index = ifor j in range(i+1, n):if array[min_index] > array[j]:min_index = j# Swaparray[i], array[min_index] = array[min_index], array[i]if __name__ == '__main__':# Ask user for size of arrayn = int(input("Enter size: "))# Create arrayarray = list()# Generate n random integers and fill arrayfor i in range(n):array.append(random.randint(-100,100)) # between -100 and 100# Print original arrayprint("Original Array", array)# Sortselection_sort(array)# Print sorted arrayprint("Sorted Array:", array)input()`

QUESTION 3

`import randomimport timedef bubble_sort(array):"""Given an array (list of integers), sort it using bubble sort"""n = len(array) # size of array# Loop through the elementsfor i in range(n):# Loop through the elements from 0 to n-i-1for j in range(n-i-1):if array[j] > array[j+1]: # the element on the left is higher than the element on the right# swaparray[j], array[j+1] = array[j+1], array[j]if __name__ == '__main__':# Create running_time variablerunning_time = 0# Ask user for size of arrayn = int(input("Enter size: "))# Variable to store the average running timeavg_time = 0# Begin with simulation the process 1000 timesfor _ in range(1000):# Create arrayarray = list()# Generate n random integers and fill arrayfor i in range(n):array.append(random.randint(-100,100)) # between -100 and 100# Record start timestart_time = time.time()# Sortbubble_sort(array)# Record end timeend_time = time.time()# Calculate running time and add to avg_timerunning_time = (end_time-start_time)avg_time += running_time# Calculate avgerage running timeavg_time = avg_time / 1000.0# Printprint("The average running time for Bubble Sort is: {:.4f} ms".format(avg_time*1000.0))input()`

QUESTION 4

`import randomimport timedef selection_sort(array):"""Given an array (list of integers), sort it using selection sort"""n = len(array) # size of array# Loop through the elementsfor i in range(n):# Loop through the elements from 0 to n-i-1min_index = ifor j in range(i+1, n):if array[min_index] > array[j]:min_index = j# Swaparray[i], array[min_index] = array[min_index], array[i]if __name__ == '__main__':# Create running_time variablerunning_time = 0# Ask user for size of arrayn = int(input("Enter size: "))# Variable to store the average running timeavg_time = 0# Begin with simulation the process 1000 timesfor _ in range(1000):# Create arrayarray = list()# Generate n random integers and fill arrayfor i in range(n):array.append(random.randint(-100,100)) # between -100 and 100# Record start timestart_time = time.time()# Sortselection_sort(array)# Record end timeend_time = time.time()# Calculate running time and add to avg_timerunning_time = (end_time-start_time)avg_time += running_time# Calculate avgerage running timeavg_time = avg_time / 1000.0# Printprint("The average running time for Selection Sort is: {:.4f} ms".format(avg_time*1000.0))input()`

Related Samples

Dive into our Python Assignments samples, covering loops, functions, data structures, and advanced topics like file handling and API integration. Explore annotated code examples that simplify complex concepts, enhancing your programming skills. Excel in Python with practical assignments tailored to deepen understanding and foster academic success.