Created
November 13, 2021 15:55
-
-
Save anchitarnav/314e78477e0813bd4f695255904b5832 to your computer and use it in GitHub Desktop.
Bubble Sort Python Implementation
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
# Bubble Sort -> Python Implementation | |
# Time Complexity: O(n^2) | |
# Space Complexity: O(1) | |
def bubble_sort(array: list) -> list: | |
def swap(index1, index2): | |
nonlocal array | |
array[index1], array[index2] = array[index2], array[index1] | |
for _end in range(len(array) - 1, 0, -1): | |
for i in range(0, _end): | |
if array[i] > array[i + 1]: | |
swap(i, i + 1) | |
return array | |
if __name__ == '__main__': | |
x = bubble_sort([4, 5, 2, 0, 1, 9, 10, 10, 10, 10]) | |
print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment