[算法学习]20150409.1.插入排序

Python实现插入排序

# -*-coding: utf-8 -*-
import random
import string


def insert_sort(array):
    ''' The implementation of insertation sort '''
    for i in range(1, len(array)):
        key = array[i]
        j = i - 1
        while j >= 0 and array[j] > key:
            array[j + 1] = array[j]
            array[j] = key
            j -= 1

    return array


if __name__ == '__main__':
    array = random.sample(string.digits, 10)
    print array
    sorted_array = insert_sort(array)
    print sorted_array
点赞