Sort Algorithm-->Bubble Sort
阿新 • • 發佈:2019-02-12
Bubble sort:
Everytime index start from left,compare two elements,if left element bigger then right element,than swap them, or index move to next. After a traverse, the max element of the rest will float to the right of n,n-1,n-2’s position like bubbles.
Complexity:O(n^2)
"""
bubble sort
language:python3.5
author:zhoutonglx
"""
#original unsorted list
L = [3,44,38,5,47,15,36,26,27,2,46,4,19,50,48]
def BubbleSort(L):
length = len(L)
for i in range(length-1) :
for j in range(length-i-1) :
if L[j]>L[j+1] :
L[j],L[j+1] = L[j+1],L[j] # like swap() in c++
print(L)
if __name__ == "__main__" :
BubbleSort(L)