手机
当前位置:查字典教程网 >脚本专栏 >python >python插入排序算法实例分析
python插入排序算法实例分析
摘要:本文实例讲述了python插入排序算法。分享给大家供大家参考。具体如下:definsertsort(array):forremoved_in...

本文实例讲述了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插入排序算法实例分析】相关文章:

python 实现堆排序算法代码

python设置检查点简单实现代码

python聊天程序实例代码分享

python实现的二叉树算法和kmp算法实例

python 中文乱码问题深入分析

python 快速排序代码

python获取糗百图片代码实例

python时间整形转标准格式的示例分享

python 随机数生成的代码的详细分析

python插入排序算法的实现代码

精品推荐
分类导航