Skip to content

Commit 9f2e321

Browse files
Merge branch 'master' of https://github.com/apachecn/LeetCode
2 parents b5195d6 + 5c902e9 commit 9f2e321

14 files changed

+286
-23
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
14. https://github.com/liuchuo/LeetCode
5050
15. https://github.com/anxiangSir/SwordforOffer
5151
16. https://www.nowcoder.com/ta/coding-interviews?page=1
52-
17. 【小姐姐】刷题博客:https://www.liuchuo.net/about
52+
17. [【小姐姐】刷题博客](https://www.liuchuo.net/about)
53+
18. [公瑾的Github](https://github.com/yuzhoujr/leetcode)
5354

5455
> Please note, this repository is inspired from [KrisYu](https://github.com/KrisYu/LeetCode-CLRS-Python). However, it has been modified, added and improved to reflect our knowledge, wisdom and effort.
5556

docs/Algorithm/Sort/BubbleSort.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
# 冒泡排序
5+
from __future__ import print_function
56
def bubble_sort(l):
67
length = len(l)
78
# 外层循环 length遍,内层循环少一遍
@@ -16,4 +17,4 @@ def bubble_sort(l):
1617
if __name__ == "__main__":
1718
l = [5, 1, 9, 3, 2, 7]
1819
bubble_sort(l)
19-
print l
20+
print(l)

docs/Algorithm/Sort/InsertSort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@
55
插入排序的前提是:左边是有序的数列
66
而冒泡排序:相邻的值进行交换,一共进行n次交换
77
"""
8+
from __future__ import print_function
89

910

1011
def insert_sort(l):
1112
# 循环 除第一个数字组成的有序数组 以外的数字
1213
for i in range(1, len(l)):
1314
# 每一个数字,依次和有序数组进行比较
14-
print l[:i]
15+
print(l[:i])
1516
for j in range(len(l[:i])):
1617
if l[i] < l[j]:
1718
l[i], l[j] = l[j], l[i]
1819

1920

2021
if __name__ == "__main__":
2122
l = [5, 1, 9, 3, 2, 7]
22-
print l
23+
print(l)
2324
insert_sort(l)
2425
print("result: " + str(l))

docs/Algorithm/Sort/MergeSort.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# coding: utf-8
22

33

4+
from __future__ import print_function
45
def MergeSort(lists):
56
if len(lists) <= 1:
67
return lists
78
num = int(len(lists) / 2)
89
# 从中间,进行数据的拆分, 递归的返回数据进行迭代排序
910
left = MergeSort(lists[:num])
1011
right = MergeSort(lists[num:])
11-
print left
12-
print "*" * 20
13-
print right
14-
print "_" * 20
12+
print(left)
13+
print("*" * 20)
14+
print(right)
15+
print("_" * 20)
1516
return Merge(left, right)
1617

1718

@@ -27,9 +28,9 @@ def Merge(left, right):
2728
r += 1
2829
result += right[r:]
2930
result += left[l:]
30-
print 'result:', result
31+
print('result:', result)
3132
return result
3233

3334

3435
if __name__ == "__main__":
35-
print MergeSort([1, 2, 3, 4, 5, 6, 7, 90, 21, 23, 45])
36+
print(MergeSort([1, 2, 3, 4, 5, 6, 7, 90, 21, 23, 45]))

docs/Algorithm/Sort/QuickSort.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding:utf8
33

44

5+
from __future__ import print_function
56
def quick_sort(l, start, end):
67
i = start
78
j = end
@@ -14,12 +15,12 @@ def quick_sort(l, start, end):
1415
while i < j:
1516
# 和最右边的比较,如果>=key,然后j-1,慢慢的和前一个值比较;如果值<key,那么就交换位置
1617
while i < j and key <= l[j]:
17-
print key, l[j], '*' * 30
18+
print(key, l[j], '*' * 30)
1819
j -= 1
1920
l[i] = l[j]
2021
# 交换位置后,然后在和最左边的值开始比较,如果<=key,然后i+1,慢慢的和后一个值比较;如果值>key,那么就交换位置
2122
while i < j and key >= l[i]:
22-
print key, l[i], '*' * 30
23+
print(key, l[i], '*' * 30)
2324
i += 1
2425
l[j] = l[i]
2526
l[i] = key
@@ -32,4 +33,4 @@ def quick_sort(l, start, end):
3233
if __name__ == "__main__":
3334
l = [5, 1, 9, 3, 2, 7]
3435
quick_sort(l, 0, len(l) - 1)
35-
print l
36+
print(l)

docs/Algorithm/Sort/SelectionSort.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
选择排序的前提是:找到最大值的位置,最后才进行1次交换
66
而冒泡排序:相邻的值进行交换,一共进行n次交换
77
"""
8+
from __future__ import print_function
89

910

1011
def selection_sort(l):
@@ -17,12 +18,12 @@ def selection_sort(l):
1718
if l[j] > l[index]:
1819
index = j
1920
l[length], l[index] = l[index], l[length]
20-
print len(l) - length, l
21+
print(len(l) - length, l)
2122
length -= 1
2223

2324

2425
if __name__ == "__main__":
2526
l = [5, 1, 9, 3, 2, 7]
26-
print l
27+
print(l)
2728
selection_sort(l)
2829
print("result: " + str(l))

docs/Algorithm/Sort/ShellSort.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# coding: utf8
22

3+
from __future__ import print_function
34
def insert_sort(l, start, increment):
45
for i in range(start+increment, len(l), increment):
56
for j in range(start, len(l[:i]), increment):
67
if l[i] < l[j]:
78
l[i], l[j] = l[j], l[i]
8-
print increment, '--',l
9+
print(increment, '--',l)
910
return l
1011

1112
def shell_sort(l, increment):
@@ -20,6 +21,6 @@ def shell_sort(l, increment):
2021
if __name__ == "__main__":
2122
l = [5, 2, 9, 8, 1, 10, 3, 4, 7]
2223
increment = len(l)/3+1 if len(l)%3 else len(l)/3
23-
print "开始", l
24+
print("开始", l)
2425
l = shell_sort(l, increment)
25-
print "结束", l
26+
print("结束", l)

0 commit comments

Comments
 (0)