天天看點

常見算法的python實作(Github标星75.5k+)

我找到一個github标星75.5k+star的倉庫,把各種常見算法用python實作了,而且還有動圖示範,非常值得推薦。(黃海廣)

倉庫說明

這個倉庫用python語言實作了絕大部分算法,主要是用于教學目的,是以效率稍微低于工業界。

倉庫位址:https://github.com/TheAlgorithms/Python

内容說明

包含了常見的算法的python實作,如二叉樹、排序、查找等等。這些是算法工程師必須掌握的技能。 

檔案目錄

常見算法的python實作(Github标星75.5k+)
常見算法的python實作(Github标星75.5k+)

冒泡排序

常見算法的python實作(Github标星75.5k+)

桶排序

常見算法的python實作(Github标星75.5k+)

快速排序

排序典型代碼(這個是冒泡排序的代碼):

def bubble_sort(collection):
    """Pure implementation of bubble sort algorithm in Python


    :param collection: some mutable ordered collection with heterogeneous
    comparable items inside
    :return: the same collection ordered by ascending


    Examples:
    >>> bubble_sort([0, 5, 2, 3, 2])
    [0, 2, 2, 3, 5]


    >>> bubble_sort([])
    []


    >>> bubble_sort([-2, -45, -5])
    [-45, -5, -2]


    >>> bubble_sort([-23, 0, 6, -4, 34])
    [-23, -4, 0, 6, 34]


    >>> bubble_sort([-23, 0, 6, -4, 34]) == sorted([-23, 0, 6, -4, 34])
    True
    """
    length = len(collection)
    for i in range(length - 1):
        swapped = False
        for j in range(length - 1 - i):
            if collection[j] > collection[j + 1]:
                swapped = True
                collection[j], collection[j + 1] = collection[j + 1], collection[j]
        if not swapped:
            break  # Stop iteration if the collection is sorted.
    return collection




if __name__ == "__main__":
    import time


    user_input = input("Enter numbers separated by a comma:").strip()
    unsorted = [int(item) for item in user_input.split(",")]
    start = time.process_time()
    print(*bubble_sort(unsorted), sep=",")
    print(f"Processing time: {time.process_time() - start}")      

總結

資料結構與算法設計怎麼學?

免費的我推薦嚴蔚敏老師的資料結構課程,網上可以查到,用c語言實作,當年考博士時候學的就是這個。

收費的我推薦極客時間的《資料結構與算法之美》:http://gk.link/a/108GK ,内容挺全面,學了應該對算法有很大幫助。

算法的python實作推薦github上一個75.5k+star的倉庫,把各種常見算法用python實作了,而且還有動圖示範。

倉庫位址:

​​​https://github.com/TheAlgorithms/Python​​

适合初學者入門人工智能的路線及資料下載下傳機器學習及深度學習筆記等資料列印機器學習線上手冊深度學習筆記專輯AI基礎下載下傳(pdf更新到25集)機器學習的數學基礎專輯擷取一折本站知識星球優惠券,複制連結直接打開:https://t.zsxq.com/yFQV7am本站qq群1003271085,加入微信群請掃碼喜歡文章,點個在看      

繼續閱讀