#--coding: utf8 --
def bin_insertion_sort(arr):
arrlen = len(arr)
for i in range(1, arrlen):
insert(arr, i)
def insert(arr, i):
bin_insert(arr, 0, i - 1, i)
def bin_insert(arr, low, high, i):
# low > high是这个递归的终止条件
middle = (low + high) / 2
if low <= high:
if arr[middle] < arr[i]:
# i应该在的位置在middle后面
bin_insert(arr, middle + 1, high, i)
else:
# i应该在的位置在middle前面
bin_insert(arr, low, middle - 1, i)
else:
tmp = arr[i]
for j in reversed(range(low, i)):
arr[j + 1] = arr[j]
arr[low] = tmp
arr = [3,9,8,4,5,2,10,18,4]
bin_insertion_sort(arr)
print arr
转载于:https://my.oschina.net/jamesxu/blog/15183