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)

猜你喜欢

转载自www.cnblogs.com/citicto/p/11087242.html
今日推荐