排序算法总结

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)

  

posted @ 2017-08-17 12:38  junmo-c  阅读(131)  评论(0编辑  收藏  举报