Last active
October 1, 2019 21:12
-
-
Save mauriciogardini/dadf6cda30bf381c94275afa6c65c10e to your computer and use it in GitHub Desktop.
Bubble Sort optimized implementation in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubble_sort_optimized(array, decrescent=False): | |
if len(array) <= 1: | |
return array | |
for x in range(len(array) - 1): | |
swapped = False | |
for y in range(len(array) - x - 1): | |
if ((decrescent and array[y] < array[y + 1]) or | |
(not decrescent and array[y] > array[y + 1])): | |
array[y], array[y + 1] = array[y + 1], array[y] | |
swapped = True | |
if not swapped: | |
return array | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment