美文网首页
【2021-07-05】算法导论学习:day 1

【2021-07-05】算法导论学习:day 1

作者: 喵吃猪 | 来源:发表于2021-07-06 10:34 被阅读0次

    关键概念

    T(n): worst case - max time for sorting any input of size n
    T(n): expected time - the weighted average time for sorting input of size n

    Asymtotic Notation
    \theta \ - \,notation:\ drop\,low\,order\,terms\,and\,ignore\,leading\,constants
    Ex.\quad 3n^3+n^2-5n+6060\rightarrow \theta(n^3)

    排序算法

    十大经典排序算法 | 菜鸟教程

    Merge Sorting - 归并排序

    引用自菜鸟教程

    python实现

    def MergeSort(left, right):
        # 比较传过来的两个序列left,right,返回一个排好的序列
        i, j = 0, 0
        result = []
        while i < len(left) and j < len(right):
            if left[i] <= right[j]:
                result.append(left[i])
                i += 1
            else:
                result.append(right[j])
                j += 1
        result += left[i:]  # 这时候i或者j到了序列的最后
        result += right[j:]
    
        print(result)
        return result
    
    
    def SortByMerge(arr, size):
        if size <= 1:
            return arr
    
        i = int(size/2)
        left = SortByMerge(arr[:i], i)
        right = SortByMerge(arr[i:], size - i)
        return MergeSort(left, right)
    
    arr = [12, 11, 13, 5, 7, 6]
    print(SortByMerge(arr, len(arr)))
    

    其余重点

    递归算法分析

    • 代换法
    • 树形图法
    • 主方法

    相关文章

      网友评论

          本文标题:【2021-07-05】算法导论学习:day 1

          本文链接:https://www.haomeiwen.com/subject/qfjiultx.html