演算法是程式設計知識體系中的重要部分。當你已經掌握了一些程式設計基礎之後,必然需要了解演算法相關的知識,才能可以寫出效率更高的程式碼。而排序演算法又是演算法中非常基礎的內容。
我們用 Python 實現了一個可視(聽)化的排序演算法演示程式,給你們直觀地展示一下 氣泡排序、選擇排序、插入排序、快速排序、歸併排序5種經典排序演算法的執行過程。
記得開啟你的聲音,片尾有小彩蛋?











此程式的想法來源於一個國外視訊:
6分鐘演示15種排序演算法_嗶哩嗶哩 (゜-゜)つロ 乾杯~-bilibili
主體程式碼和視覺化部分由 @Provin.M 實現,我在其基礎上增加了音效。
視覺化部分通過著名的 Python 圖表庫 matplotlib 中的 animation 實現。音效則是使用了 pyaudio 庫。
這幾種都是經典的排序演算法,關於其實現及演算法複雜度的討論在幾乎每一本演算法書籍上都有提及,網上直接搜尋一下也可以找出無數文章,因此本篇中不再一一贅述。如果大家對演算法感興趣,可以在留言區說下,之後也會考慮寫一些演算法方面的內容。
程式碼已上傳 github。如果你對排序演算法還不熟悉,可以嘗試修改下程式碼裡的排序資料、執行速度等引數,觀察不同排序演算法的過程和效率,以便於對這些排序演算法的理解。而如果你已經對演算法有一定了解,則可以考慮在此框架上增加更多的演算法演示。
獲取程式碼,請在公眾號 Crossin的程式設計教室 裡回覆關鍵字:排序
════
其他文章及回答:
如何自學Python | 新手引導 | 精選Python問答 | Python單詞表 | 區塊鏈 | 人工智慧 | 雙11 | 嘻哈 | 爬蟲
歡迎搜尋及關注:Crossin的程式設計教室
