python&groovy冒泡排序
Posted citicto
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
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)
以上是关于python&groovy冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章