码迷,mamicode.com
首页 > 编程语言 > 详细

Python冒泡排序

时间:2016-08-15 10:19:26      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

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))

 

Python冒泡排序

标签:

原文地址:http://www.cnblogs.com/hanggegege/p/5771851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!