from datetime import datetime source = [10, 2, 9, 5, 4, 3, 7, 1, 6, 8] def bubbleSort(list: list) -> list: sorted = list print(datetime.now()) for i in list: for j in range(len(sorted)): if j == len(sorted)-1: continue if sorted[j] > sorted[j+1]: sorted[j], sorted[j+1] = sorted[j+1], sorted[j] print(datetime.now()) print(sorted) return sorted print(bubbleSort(source))
Public
Never overwrite built-in names, like list
, sum
or max
- if later someone wants to use built-in function, he will find it replaced with your variable. If you really need to use one of those names, append underscore: sum_ = 15
.
Seems like things could be organized in a better way.
Critical error! You're assigning list
to sorted
, and now they point to the same list! So not only you return sorted list, you also modify initial list in-place.
from copy import copy sorted = copy(list)
print()
is a nice way to output to stdout. But one day you'll need to not only write to stdout, but also, say, to a file. Another day you'll need to output only severe errors' messages, and nothing else. This all could be solved if using logging
module. Usually it's as easy as from logging import getLogger; log = getLogger(__name__)
.
Create new review request