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 wtf2
#!/usr/bin/python3

def bubble_sort(L):
    swap = True
    print('function', id(swap))
    while swap:
        print('while1  ', id(swap))
        swap = False
        print('while2  ', id(swap))
        for j in range(1, len(L)):
            if L[j-1] > L[j]:
                print('if1     ', id(swap))
                swap = True
                print('if2     ', id(swap))
                temp = L[j]
                L[j] = L[j-1]
                L[j-1] = temp
        print('while3  ', id(swap))
    return L

L = [2, 6, 7, 10, 3, 5, 4, 1, 9, 8]
print(bubble_sort(L))

[ngrennan@higgs ~] ./wtf2
function 139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
if1      139823131192256
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
if1      139823131192256
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
if1      139823131192224
if2      139823131192256
while3   139823131192256
while1   139823131192256
while2   139823131192224
while3   139823131192224
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

@edgan
Copy link
Author

edgan commented Feb 5, 2018

[ngrennan@higgs ~] cat ./not-wtf1
#!/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]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

L = [2, 6, 7, 10, 3, 5, 4, 1, 9, 8]
bubbleSort(L)
print(L)
[ngrennan@higgs ~] ./not-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