Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is not plotting please help import matplotlib.pyplot as plt import time # Insertion Sort def insertion _ sort ( arr ) : comparisons =

This is not plotting please help
import matplotlib.pyplot as plt
import time
# Insertion Sort
def insertion_sort(arr):
comparisons =0
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
arr[j +1]= arr[j]
j -=1
comparisons +=1
arr[j +1]= key
return comparisons
# Merge Sort
comparisons =0
def merge_sort(arr):
global comparisons
if len(arr)>1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k =0
while i < len(L) and j < len(R):
comparisons +=1
if L[i]< R[j]:
arr[k]= L[i]
i +=1
else:
arr[k]= R[j]
j +=1
k +=1
while i < len(L):
arr[k]= L[i]
i +=1
k +=1
while j < len(R):
arr[k]= R[j]
j +=1
k +=1
return comparisons
fileNames =["rand1000.txt"]
execution_times_insertion =[]
execution_times_merge =[]
n_comparisons_insertion =[]
n_comparisons_merge =[]
sizes =[]
for name in fileNames:
with open(name,'r') as f:
arr =[int(num) for line in f for num in line.split()]
sizes.append(len(arr)) # Store the size of the dataset
arr_copy = arr.copy()
# Measure execution time and number of comparisons for insertion sort
start_time = time.time()
comparisons = insertion_sort(arr)
end_time = time.time()
execution_times_insertion.append(end_time - start_time)
n_comparisons_insertion.append(comparisons)
# Measure execution time and number of comparisons for merge sort
start_time = time.time()
comparisons = merge_sort(arr_copy)
end_time = time.time()
execution_times_merge.append(end_time - start_time)
n_comparisons_merge.append(comparisons)
# Plotting the results
plt.figure(figsize=(12,6))
plt.plot(sizes, execution_times_insertion, label='Insertion Sort Execution Time')
plt.plot(sizes, execution_times_merge, label='Merge Sort Execution Time')
plt.xlabel('Dataset Size')
plt.ylabel('Execution Time (seconds)')
plt.title('Execution Time vs Dataset Size')
plt.legend()
plt.show()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

Students also viewed these Databases questions

Question

=+3. How does this message use an emotional appeal?

Answered: 1 week ago

Question

1. In what ways has flexible working revolutionised employment?

Answered: 1 week ago