import matplotlib.pyplot as plt import random import time def insertion_sort(arr): n=len(arr) for i in range(1,4): key=arr[i] j=i-1 while j>=0 and arr[j]>key: arr[j+1]=arr[j] j-=1 arr[j+1] def generatelist(n): return [random.randint(1,10000) for i in range(n)] def measuretime(n): arr=generatelist(n) starttime=time.time insertion_sort(arr) endtime=time.time() return endtime-starttime def plotGraph(n_list): timelist=[measuretime(n) for n in list] plt.plot(n_list,timelist,'0-') plt.xlabel('n') plt.ylabel('time(s)') plt.title('Insertion sort') plt.show() nlist=[100,500,1000,2000,5000,10000] plotgraph(n_list)
line 21 def plotGraph(n list): ^^^^ SyntaxError: invalid syntax
import matplotlib.pyplot as plt import random import time def insertion_sort(arr): n = len(arr) for i in range(1, n): # Changed to n to sort the entire array key = arr[i] j = i - 1 while j >= 0 and arr[j] > key: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key # Fixed to assign key to the correct position def generatelist(n): return [random.randint(1, 10000) for i in range(n)] def measuretime(n): arr = generatelist(n) starttime = time.time() # Fixed to call time.time() insertion_sort(arr) endtime = time.time() return endtime - starttime def plotGraph(n_list): timelist = [measuretime(n) for n in n_list] # Fixed to use n_list plt.plot(n_list, timelist, '0-') plt.xlabel('n') plt.ylabel('time(s)') plt.title('Insertion sort') plt.show() n_list = [100, 500, 1000, 2000, 5000, 10000] plotGraph(n_list) # Fixed to call plotGraph correctly
### Changes Made: 1. Fixed the function name `plotgraph` to `plotGraph`. 2. Corrected the range in `insertion_sort` to iterate through the entire array. 3. Assigned `key` to the correct position in `insertion_sort`. 4. Called `time.time()` correctly in `measuretime`. 5. Used `n_list` instead of `list` in `plotGraph`.