python插入排序算法實例分析
更新時間:2015年07月03日 09:28:02 作者:pythoner
這篇文章主要介紹了python插入排序算法,通過兩個簡單實例對比分析了Python插入排序算法的相關實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了python插入排序算法。分享給大家供大家參考。具體如下:
def insertsort(array):
for removed_index in range(1, len(array)):
removed_value = array[removed_index]
insert_index = removed_index
while insert_index > 0 and array[insert_index - 1] > removed_value:
array[insert_index] = array[insert_index - 1]
insert_index -= 1
array[insert_index] = removed_value
另外一個版本:
def insertsort(array):
for lastsortedelement in range(len(array)-1):
checked = lastsortedelement
while array[checked] > array[lastsortedelement + 1] and checked >= 0:
checked -= 1
#Insert the number into the correct position
array[checked+1], array[checked+2 : lastsortedelement+2] = array[lastsortedelement+1], array[checked+1 : lastsortedelement+1]
return array
希望本文所述對大家的Python程序設計有所幫助。
您可能感興趣的文章:
- python 實現(xiàn)插入排序算法
- python插入排序算法的實現(xiàn)代碼
- Python實現(xiàn)快速排序和插入排序算法及自定義排序的示例
- Python中使用插入排序算法的簡單分析與代碼示例
- 淺談插入排序算法在Python程序中的實現(xiàn)及簡單改進
- Python實現(xiàn)的插入排序算法原理與用法實例分析
- Python排序搜索基本算法之選擇排序實例分析
- Python排序搜索基本算法之冒泡排序實例分析
- Python排序搜索基本算法之希爾排序實例分析
- Python排序搜索基本算法之歸并排序實例分析
- Python實現(xiàn)基于二叉樹存儲結構的堆排序算法示例
- Python排序搜索基本算法之插入排序實例分析
相關文章
Python采用Django開發(fā)自己的博客系統(tǒng)
這篇文章主要為大家詳細介紹了Python采用Django開發(fā)自己的博客系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08

