您的位置:首页 > 编程语言 > Go语言

Sort Algorithm-->Bubble Sort

2016-07-24 20:16 441 查看

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: