Skip to content

Commit f7891b9

Browse files
committed
modify sort
1 parent 57669d3 commit f7891b9

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

‎Algorithm/sort/bubble-sort.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44

55
defbubble_sort(L):
6-
iflen(L) <2:
6+
'''
7+
冒泡排序主要使用两次循环实现排序。外循环中的一个数字依次与内层循环中的每个数字进行比较,如果索引值小的数字大于索引值大的数字,交换位置。否则,不变。直至循环结束
8+
'''
9+
iflen(L) <2: # 列表内元素低于2,直接返回!
710
returnL
811
foriinrange(len(L)):
912
forjinrange(1, len(L)):

‎Algorithm/sort/quick-sort.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# coding=utf-8
22
importrandom
33

4-
defquick_sort(L):
5-
ifL== []:
6-
returnL
7-
mid=random.choice(L)
8-
small= [xforxinLifx<mid]
9-
big= [xforxinLifx>mid]
4+
defquick_sort(seq):
5+
iflen(seq) <2:
6+
returnseq
7+
8+
mid=random.choice(seq)
9+
small= [xforxinseqifx<mid]
10+
big= [xforxinseqifx>mid]
1011
returnquick_sort(small) + [mid] +quick_sort(big)
1112

1213
if__name__=='__main__':
13-
L= [23, 3, 45, 1, 6, 89, 345, 8, 345]
14+
L= [random.randrange(1000) for_inrange(10)]
1415
print(quick_sort(L))

0 commit comments

Comments
(0)