Python冒泡排序

c实现冒泡排序:

void bubble_sort(int a[], int n)
{
    int i, j, temp;
    for (j = 0; j < n - 1; j++)
        for (i = 0; i < n - 1 - j; i++)
         if(a[i] > a[i + 1])
            {
                temp=a[i]; 
                a[i]=a[i+1]; 
                a[i+1]=temp;}
}

Python冒泡排序:

array = [1,2,3,6,5,4]
bubble_sort(array)
def bubble_sort(l):
for i in range(len(l), 0, -1):
for j in range(len(l) - 1):
if l[j] > l[j + 1]:
tmp = l[j]
l[j] = l[j + 1]
l[j + 1] = tmp

print("result: " + str(l))

 

posted @ 2016-08-15 09:51  航飞冲天  阅读(234)  评论(0编辑  收藏  举报