Skip to content

Instantly share code, notes, and snippets.

@nolifeasian24-7
Created October 9, 2020 16:22
Show Gist options
  • Save nolifeasian24-7/d08d04ec90dd75a7316dde3753d8ccb3 to your computer and use it in GitHub Desktop.
Save nolifeasian24-7/d08d04ec90dd75a7316dde3753d8ccb3 to your computer and use it in GitHub Desktop.
The bubble sort algorithm in python
def start():
arr = [64,34,25,12,22,11,90]#an array of numbers
lenght = len(arr)#this gets the amount of element (items) in this array
for i in range(lenght-1):#looping through this array
for j in range(0, lenght-i-1):#this then loops through each sought index
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]#these lines compare the indexes and acts accordingly (so if one element is smaller then the other then it will switch places in the list)
print("your sorted array is: ")
for i in range (len(arr)):
print("%d" %arr[i])#print the sorted array
if i == len(arr):
exit(0)#will exit once the array is sorted.
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment