首页 > 文章 > python&groovy冒泡排序

python&groovy冒泡排序

无论天空有多高,踮起双脚就能靠近太阳

Groovy:

直接用sort():

class Example {
   static void main(String[] args) {
      def lst = [13, 12, 15, 14];
      def newlst = lst.sort();
      println(newlst);
   }
}

 

Python:

def bubble_sort(alist):
    n = len(alist)
    for i in range(0, n - 1):
        count = 0
        for j in range(0, n-i-1):
            if alist[j] > alist[j + 1]:
                alist[j], alist[j + 1] = alist[j + 1], alist[j]
                count += 1
        if 0 == count:
            break

if __name__ == "__main__":
    alist = [1, 3, 19, 29, 35, 56, 89]
    print(alist)
    bubble_sort(alist)
    print(alist)
时间:2019-06-25 23:15:27阅读(53)
联系我们 - 留言反馈
© 2017 版权所有 鲁ICP备17052893号