Skip to content

Instantly share code, notes, and snippets.

@edgan
Created February 5, 2018 16:38
Show Gist options
  • Save edgan/3cea4c1979adbe75ae052d402ae9a892 to your computer and use it in GitHub Desktop.
Save edgan/3cea4c1979adbe75ae052d402ae9a892 to your computer and use it in GitHub Desktop.
[ngrennan@higgs ~] cat wtf1
#!/usr/bin/python3
def bubble_sort(L):
swap = False
while not swap:
swap = True
for j in range(1, len(L)):
if L[j-1] > L[j]:
swap = False
temp = L[j]
L[j] = L[j-1]
L[j-1] = temp
return L
L = [2, 6, 7, 10, 3, 5, 4, 1, 9, 8]
print(bubble_sort(L))
[ngrennan@higgs ~] ./wtf1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
@edgan
Copy link
Author

edgan commented Feb 5, 2018

[ngrennan@higgs ~] cat not-wtf2
#!/usr/bin/python3

def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                alist[i], alist[i+1] = alist[i+1], alist[i]

L = [2, 6, 7, 10, 3, 5, 4, 1, 9, 8]
bubbleSort(L)
print(L)
[ngrennan@higgs ~] ./not-wtf2
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

@edgan
Copy link
Author

edgan commented Feb 5, 2018

[ngrennan@higgs ~] cat ./not-wtf3
#!/usr/bin/python3

def bubbleSort(alist):
    for i in range(len(alist)-1):
        for i in range(len(alist)-1):
            if alist[i] > alist[i+1]:
                alist[i], alist[i+1] = alist[i+1], alist[i]

L = [2, 6, 7, 10, 3, 5, 4, 1, 9, 8]
bubbleSort(L)
print(L)
[ngrennan@higgs ~] ./not-wtf3
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment