#!/usr/bin/env python # -*- coding: utf-8 -*- # 插入排序 # 时间复杂度 O(n^2) import time def logger(func): start_time = time.time() def inner(*args, **kwargs): # 1 return func(*args, **kwargs) # 2 print(time.time() - start_time) return inner @logger def insertion_sort(array): length = len(array) for i in range(1, length): for m in range(i): if array[i] < array[m]: array[i], array[m] = array[m], array[i] if __name__ == '__main__': aa = [1, 4, 3, 5, 6, 2, 6, 0, 0] insertion_sort(aa) print(aa)
package main // 插入排序 // 时间复杂度 O(n^2) import "fmt" func insertionSort(array []int) { length := len(array) for i := 1; i < length; i ++ { for m := 0; m < i; m ++ { if array[i] > array[m] { array[i], array[m] = array[m], array[i] } } } } func main() { aa := []int{1, 4, 3, 5, 6, 2, 6, 0, 0} insertionSort(aa) fmt.Println(aa) }