Python与Go冒泡排序
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # 冒泡排序法 4 5 6 def bubbling(array): # 时间复杂度:O(n^2) 7 for i in range(0, len(array) - 1): 8 for m in range(i + 1, len(array)): 9 if array[i] < array[m]: 10 array[i], array[m] = array[m], array[i] 11 12 13 if __name__ == '__main__': 14 aa = [1, 4, 3, 5, 6, 2, 6, 0, 0] 15 print(aa)
1 package main 2 3 import "fmt" 4 // 冒泡排序法 5 // 时间复杂度:O(n^2) 6 func bubbling(array []int) { 7 length := len(array) 8 for i := 0; i < length - 1; i ++ { 9 for m := 1; m < length; m ++ { 10 if array[m] > array[m -1] { 11 array[m],array[m -1] = array[m - 1], array[m] 12 } 13 } 14 } 15 } 16 17 func main() { 18 aa := []int{1, 4, 3, 5, 6, 2, 6, 0, 0} 19 bubbling(aa) 20 fmt.Println(aa) 21 }