码迷,mamicode.com
首页 > 编程语言 > 详细

Python - 排序

时间:2019-05-25 15:43:33      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:pre   temp   查找   image   简单   shuff   pos   图片   main   

插入排序

算法分析

技术图片

 

简单实例

import random


def foo(l):
    for i in range(1, len(l)):
        temp = l[i]
        pos = i
        for j in range(i - 1, -1, -1):
            if temp < l[j]:
                l[j + 1] = l[j]
                pos = j
            else:
                pos = j + 1
                break

        l[pos] = temp
    return l


if __name__ == __main__:
    l = list(range(13))
    random.shuffle(l)
    print(l)  # [12, 0, 4, 5, 6, 2, 11, 10, 8, 7, 3, 1, 9]
    print(foo(l))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

 

 

 

冒泡排序

二分查找

快速排序

 

Python - 排序

标签:pre   temp   查找   image   简单   shuff   pos   图片   main   

原文地址:https://www.cnblogs.com/shijieli/p/10922566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!