Answered step by step
Verified Expert Solution
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 insertionsortarr:
comparisons
for i in range lenarr:
key arri
j i
while j and key arrj :
arrj arrj
j
comparisons
arrj key
return comparisons
# Merge Sort
comparisons
def mergesortarr:
global comparisons
if lenarr:
mid lenarr
L arr:mid
R arrmid:
mergesortL
mergesortR
i j k
while i lenL and j lenR:
comparisons
if Li Rj:
arrk Li
i
else:
arrk Rj
j
k
while i lenL:
arrk Li
i
k
while j lenR:
arrk Rj
j
k
return comparisons
fileNames randtxt
executiontimesinsertion
executiontimesmerge
ncomparisonsinsertion
ncomparisonsmerge
sizes
for name in fileNames:
with opennamer as f:
arr intnum for line in f for num in line.split
sizes.appendlenarr # Store the size of the dataset
arrcopy arr.copy
# Measure execution time and number of comparisons for insertion sort
starttime time.time
comparisons insertionsortarr
endtime time.time
executiontimesinsertion.appendendtime starttime
ncomparisonsinsertion.appendcomparisons
# Measure execution time and number of comparisons for merge sort
starttime time.time
comparisons mergesortarrcopy
endtime time.time
executiontimesmerge.appendendtime starttime
ncomparisonsmerge.appendcomparisons
# Plotting the results
pltfigurefigsize
pltplotsizes executiontimesinsertion, label'Insertion Sort Execution Time'
pltplotsizes executiontimesmerge, label'Merge Sort Execution Time'
pltxlabelDataset Size'
pltylabelExecution Time seconds
plttitleExecution Time vs Dataset Size'
pltlegend
pltshow
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started