How can this algorithm be improved? I mean, a list with 3000 items takes about 5 seconds to sort, according to Sublime Text. Using Python's sorted
function is immediate.
from random import randint
def bubble_sort(iterable):
while True:
corrected = False
for item in range(0, len(iterable) - 1):
if iterable[item] > iterable[item + 1]:
iterable[item], iterable[item + 1] = iterable[item + 1], iterable[item]
corrected = True
if not corrected:
return iterable
if __name__ == '__main__':
random_list = [randint(0, 100) for _ in range(3000)]
print(bubble_sort(random_list))
sorted
function uses theTimsort
algorithm (last time I checked) which has a time complexity of O(n log n) \$\endgroup\$