首页 > 编程语言 > 详细

Python冒泡排序

时间:2016-08-15 10:19:20      阅读:197      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!