The time complexity of any of the sorting algorithm and represent it graphically
Algorithm:
Algorithm BubbleSort(array,size)
start:=time;
for i:=0 to n do
{
for j:=i+1 to n do
{
if (array[i]>array[j]) then
{
temp = array[i]; array[i] = array[j]; array[j] = temp;
}
}
}
end:=time;
Source Code:
import time import random
import matplotlib.pyplot as plt
import numpy as np
def bubble_sort_time(n):
start = time.time()
array = []
for i in range(n):
rannum = random.randint(1, n) array.append(rannum)
leng = len(array) for i in range(leng):
for j in range(i, leng):
if (array[i] > array[j]): temp = array[i] array[i] = array[j] array[j] = temp
end = time.time()
print("Execution Time for Sorting", n,
"Values:", end-start) return end - start
exetime = [bubble_sort_time(n) for n in range(1000, 7000, 1000)]
x = np.array([1000, 2000, 3000, 4000, 5000, 6000])
plt.title("Linear Graph for Execution Time of Bubble
Sort")
plt.xlabel("Values")
plt.ylabel("Execution Time") plt.plot(x, exetime, marker='o')
plt.show()
Comments
Post a Comment