(給演演算法愛好者加星標,修煉程式設計內功)
綜合整理:Python開發者(id:PythonCoder)
4 月 28日,GitHub 趨勢榜第 1 位是一個用 Python 編碼實現的演演算法庫,Star 數早已達到 26000+

連結:https://github.com/TheAlgorithms/Python
這個庫涵蓋了多種演演算法和資料結構的介紹,比如:
-
排序演演算法(氣泡排序、希爾排序、插入排序、桶排序、合併排序、快速排序、堆排序、選擇排序等)、
-
查詢演演算法(線性查詢、二分查詢、插值查詢等)
-
加密演演算法(凱撒加密、RSA、ROT13 等)
-
機器學習
-
圖
-
數字影象處理
-
動態規劃
-
常見資料結構(佇列、棧、連結串列、樹等)

這個庫雖然包括的種類很多,但內容其實分了 2 方面:① 演演算法的原理簡介;② 演演算法的程式碼實現;
比如:冒泡演演算法的 Python 實現
from __future__ import print_functiondef bubble_sort(collection):"""Pure implementation of bubble sort algorithm in Python:param collection: some mutable ordered collection with heterogeneouscomparable items inside:return: the same collection ordered by ascendingExamples:>>> bubble_sort([0, 5, 3, 2, 2])[0, 2, 2, 3, 5]>>> bubble_sort([])[]>>> bubble_sort([-2, -5, -45])[-45, -5, -2]>>> bubble_sort([-23,0,6,-4,34])[-23,-4,0,6,34]"""length = len(collection)for i in range(length-1):swapped = Falsefor j in range(length-1-i):if collection[j] > collection[j+1]:swapped = Truecollection[j], collection[j+1] = collection[j+1], collection[j]if not swapped: break # Stop iteration if the collection is sorted.return collectionif __name__ == '__main__':try:raw_input # Python 2except NameError:raw_input = input # Python 3user_input = raw_input('Enter numbers separated by a comma:').strip()unsorted = [int(item) for item in user_input.split(',')]print(*bubble_sort(unsorted), sep=',')
感興趣的童鞋,請收藏:
https://github.com/TheAlgorithms/Python
知識星球
朋友會在“發現-看一看”看到你“在看”的內容