python insertion sort
2011-11-03 21:53
253 查看
直接插入排序(insertion sort)
思想:用关键码依次比较待排序元素位置前面的关键码,找到要插入的位置,其后的元素依次后移。
时间复杂度:o(n*n)
关键码比较次数:n*n/2
对象移动次数:n*n/2
稳定性:稳定
思想:用关键码依次比较待排序元素位置前面的关键码,找到要插入的位置,其后的元素依次后移。
时间复杂度:o(n*n)
关键码比较次数:n*n/2
对象移动次数:n*n/2
稳定性:稳定
#!/urs/bin/env python # -*- encoding:utf-8 -*- def insertsort(seq): for i in range(1, len(seq)): j = i - 1 temp = seq[i] while seq[j]>temp and j >= 0: seq[j+1] = seq[j] j = j - 1 seq[j+1] = temp if __name__ == '__main__': seq = [5, 2, 1, 3, 4, 8, 1] insertsort(seq) print seq
相关文章推荐
- Insertion Sort List Leetcode Python
- Python 数据结构与算法——插入排序(insertion sort)
- [LeetCode]题解(python):147-Insertion Sort List
- [Leetcode][python]Insertion Sort List
- LeetCode 147 — Insertion Sort List(C++ Java Python)
- 【LeetCode with Python】 Insertion Sort List
- [硕.Love Python] InsertionSort(插入排序)
- 【LEETCODE】147- Insertion Sort List [Python]
- [硕.Love Python] InsertionSort(插入排序)
- 插入排序(INSERTION-SORT) PYTHON实现
- insertion mergesort bubble sort 复习 python
- [leetcode] Insertion Sort List(python)
- [leetcode] Insertion Sort List(python)
- LeetCode-Insertion Sort List (Python)
- [leetcode]Insertion Sort List @ Python
- leetcode 【 Insertion Sort List 】 python 实现
- 使用python实现排序算法(Insertion Sort)
- 1098. Insertion or Heap Sort (25)
- python中List的sort方法指南
- leetcode - Insertion Sort List