首页 > 编程语言 > 详细

排序算法总结

时间:2017-08-17 13:33:31      阅读:159      评论:0      收藏:0      [点我收藏+]

1.插入排序

循环数组,从第二个开始,和前面的比较,找到它的位置插入他的指定位置

def insertSort(res):
    length = len(res)
    for i in range(1,length):
        temp = res[i]
        index = 0
        for j in range(i)[::-1]:
            if res[j]>temp:
                res[j+1] = res[j]
            else:
                index = j+1
                break
        res[index] = temp

    return res

  2.归并排序

选择一个基准值,把序列分成两个,在合并起来,递归完成,当最小序列长度小于等于1,直接返回该序列

def quickSort(res):
    if len(res)<=1:
        return res
    length = len(res)
    standard = res[0]
    lres = []
    rles = []
    for i in res:
        if i<standard:
            lres.append(i)
        elif i>standard:
            rles.append(i)
    return quickSort(lres)+[standard]+quickSort(rles)

  

排序算法总结

原文:http://www.cnblogs.com/cshunter/p/7380979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!