zoukankan      html  css  js  c++  java
  • 插入排序

    def insertionSort(arr):
       for i in range(len(arr)):
           preIndex = i-1
           current = arr[i]
           while preIndex >= 0 and arr[preIndex] > current:
               arr[preIndex+1] = arr[preIndex]
               preIndex-=1
           arr[preIndex+1] = current
       return arr


    1. 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。

    2. 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。)

  • 相关阅读:
    2.25家庭记账本小软件
    2.10简单体温记录小软件总结
    4.26PHP
    4.25Android
    4.24css
    4.23css
    4.22电梯演讲
    4.21python
    4.20python
    4.19python
  • 原文地址:https://www.cnblogs.com/heruxiu/p/14751572.html
Copyright © 2011-2022 走看看