python写的二分插入算法

def main():
    arr= (5,2,1,83,491,3,45,21,56,235,61,36,13,4,643,123,64)
    list = []
    for node in arr:
        end = len(list)
        start = 0
        medium = end // 2
        if 0 != end:
            while True:
                if node > list[medium]:
                    start = medium + 1;
                else:
                    end = medium;

                medium = start + (end - start) / 2//end - start为基数时,除以2取整后会损失精度,我认为这是start = medium + 1的原因;
                if start >= end:
                    break;

        list.insert(start, node)
        for a in list:
            print a;
        print "======="


if __name__ == '__main__':
    main()

 

posted @ 2013-03-26 19:45  wsjisji  阅读(912)  评论(0编辑  收藏  举报