python---插入排序
这个方法,不错的呢~~~
看看后面还有没有更好的。
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。如果没有,那最好用这种。
# coding = utf-8
'''
# ========插入排序(用了两个列表)========
def insert_sort(a_list):
loop_count = 0
insert_count = 0
insert_list = [a_list[0]]
for i in range(1, len(a_list)):
loop_count += 1
for j in range(len(insert_list)):
loop_count += 1
if a_list[i] > insert_list[j]:
insert_count += 1
insert_list.insert(j, a_list[i])
break
print('======== insert_sort loop_count========', loop_count)
print('========insert_sort insert_count=========', insert_count)
return insert_list
'''
# ========插入排序(用了一个列表)========
def insert_sort(a_list):
loop_count = 0
for index in range(1, len(a_list)):
current_value = a_list[index]
pos = index
loop_count += 1
while pos > 0 and a_list[pos-1] > current_value:
loop_count += 1
a_list[pos] = a_list[pos-1]
print(a_list)
pos -= 1
a_list[pos] = current_value
print('======== insert_sort loop_count========', loop_count)
my_list = [4, 5, 7, 2, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36]
insert_sort(my_list)
print(my_list)
D:\cheng\test\Scripts\python.exe D:/GIT/Prism4K/Prism4K/document/tests.py [4, 5, 7, 7, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [4, 5, 5, 7, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [4, 4, 5, 7, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 9, 9, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 54, 765, 765, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 54, 54, 765, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 45, 54, 765, 9876, 9876, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 45, 54, 765, 765, 9876, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 45, 54, 54, 765, 9876, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 45, 45, 54, 765, 9876, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 34, 45, 54, 765, 9876, 12343, 12343] [2, 4, 5, 7, 7, 9, 9, 34, 45, 54, 765, 9876, 9876, 12343] [2, 4, 5, 7, 7, 9, 9, 34, 45, 54, 765, 765, 9876, 12343] [2, 4, 5, 7, 7, 9, 9, 34, 45, 54, 54, 765, 9876, 12343] [2, 4, 5, 7, 7, 9, 9, 34, 45, 45, 54, 765, 9876, 12343] ======== insert_sort loop_count======== 28 [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 9876, 12343] Process finished with exit code 0
更多精彩

