都说插入排序的时间复杂度是 O(n^2)=ax^2 + bx + c,可这里的 a,b,c 各是多少?下面是插入排序的Python实现。第一个是正常版的,第二个是用递归实现。 #!/usr/bin/python # Use python 2.x # # Insertion sort # 2009-12-17 # def insertion_sort(aa): for j in range(0, aa.__len__()): key = aa[j] i = j-1 while i >= 0 and aa[i] > key: aa[i+1] = aa[i] i = i - 1 aa[i+1] = key return aa #### End of def insertion_sort def insertion_sort_recursive(a, r): if r == 1: pass else: insertion_sort_recursive(a, r - 1) print a t = a[r-1] print 't is ', t i = 0 while i < r and a[i] < t: i += 1 j = r - 1 while j > i: a[j] = a[j-1] j -= 1 a[i] = t ### End of def insertion_sort_recursive