I saw in Wikipedia (the first under Optimizing bubble sort) a pseudo-code to bubble sort and I implemented it in Python.
I am posting my implementation here because I'm new to Python, and I'm looking mostly for programming in a more pythonic way.
def bubbleSort(array): n = len(array) swapped = True while swapped: swapped = False for i in range(n - 1): if array[i] > array[i + 1]: array[i], array[i + 1] = array[i + 1], array[i] swapped = True n -= 1 return array def main(): array = [1, 7, 4, 3, 2, 9, 8, 5, 6] array = bubbleSort(array) print(array) if __name__ == '__main__': main()
I'd love to know if there are any stuff that I can optimize (syntactic-wise), like the fact that I'm writing
swapped = True, and afterward changing it again, and again ... which thing annoys me.