-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.py
More file actions
40 lines (32 loc) · 980 Bytes
/
insert.py
File metadata and controls
40 lines (32 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 24 17:24:54 2016
@author: luzhangqin
"""
#排序算法
#插入排序
#平均时间O(n^2)
#最差情形O(n^2)
#稳定度:稳定
#额外空间O(1)
#备注:大部分已排序时较好
'''
有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,
但要求插入此数据之后序列依旧有序。
刚开始的一个元素显然有序,然后插入一个元素到适当位置,
然后插入一个元素到适当位置,然后再插入第三个元素,依次类推。
'''
def insert_sort(nums):
nums_len = len(nums)
for i in range(nums_len):
if i == 0:
pass
else:
for x in range(i):
if nums[x] > nums[i]:
nums[x], nums[i] = nums[i], nums[x]
if __name__ == '__main__':
nums = [10, 8, 4, -1, 2, 6, 7, 3]
print('nums is: ', nums)
insert_sort(nums)
print('insert_sort: ',nums)