'Selection sort vs Bubble sort algorithm
I am learning the time complexity of bubble sort and selection sort using Python. In theory selection sort is said to be faster than bubble sort ,however my programs written in Python indicates both of them take almost the same time for execution.
My graphs:
my code:
import numpy
import time
import matplotlib.pyplot as plt
n = 1000
selection = {}
bubble = {}
while n<=10000:
a = numpy.random.randint(low=1 , high= 100 , size= n)
#selection sort
start = time.time()
for i in range(n-1):
idx = i
for j in range(i+1 , n):
if a[j] < a[idx]:
idx = j
a[i] , a[idx] = a[idx] , a[i]
end = time.time()
selection[n] = end-start
#bubble sort
start = time.time()
for i in range(n-1):
for j in range(n-1-i):
if a[j] > a[j+1]:
a[j] , a[j+1] = a[j+1] , a[j]
end = time.time()
bubble[n] = end-start
n+=1000
print(selection , bubble)
plt.plot(selection.keys() , selection.values() , label="selectionSort")
plt.plot(bubble.keys() , bubble.values() , label="bubbleSort")
plt.xlabel("n")
plt.ylabel("time")
plt.show()
Can anyone help me understand this abomination?
Edit: problem was I was giving sorted array to bubble sort
New graph for the record:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|



