MDGSF Software Engineer

[python] bubble sort

2017-08-28

"""
This is bubble
"""

#from __future__ import print_function

def bubble_sort(collection):
    length = len(collection)
    for i in range(length - 1, -1, -1):
        for j in range(i):
            if collection[j] > collection[j+1]:
                collection[j], collection[j+1] = collection[j+1], collection[j]

    return collection


def bubble_while(collection):
    i = len(collection) - 1
    while i >=0 :
        j = 0
        while j < i - 1:
            if collection[j] > collection[j+1]:
                collection[j], collection[j+1] = collection[j+1], collection[j]
            j = j + 1
        i = i - 1

    return collection


L = [2,3,1,3,5,542,5,45,3,3]
print(bubble_sort(L))
print(bubble_while(L))

weixingongzhonghao

上一篇 [python] 切片

Comments

Content