forked from z2labplus/algorithm-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.py
More file actions
43 lines (38 loc) · 968 Bytes
/
Copy pathbubble_sort.py
File metadata and controls
43 lines (38 loc) · 968 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
41
42
43
"""
python3.6.4
@Date : "2018-10-15"
@Author :"HerryZhang"
#Reference :https://time.geekbang.org/column/article/41802
"""
def bubble_sort(raw_list):
"""
冒泡排序
"""
flag = False
list_len = len(raw_list)
for i in range(list_len):
for j in range(list_len - i - 1):
if raw_list[j] > raw_list[j + 1]:
tmp = raw_list[j]
raw_list[j] = raw_list[j + 1]
raw_list[j + 1] = tmp
flag = True
if not flag:
break
return raw_list
def insertion_sort(raw_list):
"""
插入排序
"""
list_len = len(raw_list)
if list_len <= 1:
return
for i in range(1, list_len):
value = raw_list[i] # 1
for j in range(i, -1, -1):
if raw_list[j - 1] > value:
raw_list[j] = raw_list[j - 1]
else:
break
raw_list[j] = value
return raw_list