Skip to content

Commit fee8069

Browse files
authored
Merge pull request #4 from shengexing/93710
93710
2 parents a54240a + f8bb8d8 commit fee8069

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

AlgorithmDiagram9787115447630/Chapter01/c01_2_binarySearch/binarySearch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def binary_search(list, item):
1111
guess = list[mid]
1212
if guess == item: # 找到了元素
1313
return mid
14-
if guess > item: # 猜的数字大了
14+
elif guess > item: # 猜的数字大了
1515
high = mid - 1
1616
else: # 猜的数字小了
1717
low = mid + 1

AlgorithmDiagram9787115447630/Chapter04/c04_1_1_sum/maxArray.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
# 对列表元素求最大值
55
def maxArray(num):
66
if num:
7-
temp0 = num.pop() # 数组不为空,取出最后一个元素
7+
temp0 = num.pop() # 列表不为空,取出最后一个元素
88
if num:
99
temp1 = maxArray(num)
1010
if temp0 < temp1:
1111
return temp1
1212
return temp0
1313
else:
14-
return None # 数组为空,返回 None
14+
return None # 列表为空,返回 None
1515

1616

1717
print(maxArray([]))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
""" 快速排序 """
2+
3+
import random
4+
5+
6+
# 快速排序的函数
7+
def quickSort(array):
8+
if len(array) < 2:
9+
return array # 基线条件:为空或只包含一个元素的数组是 “有序” 的
10+
else:
11+
index = random.randint(0, len(array) - 1)
12+
return quickSort(
13+
[i for i in array[0:index] + array[index+1:] if i < array[index]]
14+
) + [array[index]] + quickSort(
15+
[i for i in array[0:index] + array[index+1:] if i >= array[index]]
16+
)
17+
18+
19+
print(quickSort([]))
20+
print(quickSort([5]))
21+
print(quickSort([5, 3]))
22+
print(quickSort([5, 3, 6, 2, 10]))

0 commit comments

Comments
 (0)