Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python算法之插入排序

初学Python,写一些算法作为练手。输入‘e" 作为输入的结束Python算法之插入排序代码:def insert_sort(seq):
    length = len(seq)
    for i in range(1,length):
        temp = seq[i]
        for j in range(i-1, -1, -1):
            if temp < seq[j]:
                seq[j+1]=seq[j]     
            else:
                j+=1
                break
        if seq[j] is not temp:
            seq[j]=temp
           
def do_test():
    seq = []
    print("Please input the sequence:")
    while True:
        ch = input()
        if ch is "e":
            break
        seq.append(int(ch)) 
    insert_sort(seq)
    print("After the insertSort:")
    print(seq)if __name__ == "__main__":
    do_test()推荐阅读:Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htmPython文件处理:读取文件 http://www.linuxidc.com/Linux/2013-08/88496.htm如何发布自定义的Python模块 http://www.linuxidc.com/Linux/2013-08/88495.htmPython爬虫多线程抓取代理服务器 http://www.linuxidc.com/Linux/2013-07/87289.htm