Skip to content

Instantly share code, notes, and snippets.

@anchitarnav
Created November 13, 2021 15:55
Show Gist options
  • Save anchitarnav/314e78477e0813bd4f695255904b5832 to your computer and use it in GitHub Desktop.
Save anchitarnav/314e78477e0813bd4f695255904b5832 to your computer and use it in GitHub Desktop.
Bubble Sort Python Implementation
# 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