Skip to content

Commit

Permalink
Fix quicksort in border condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tobegit3hub committed Apr 2, 2016
1 parent 42c02c2 commit 6f5d7c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions 001快速排序.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ quicksort(a)
def quicksort(a):
first = a[0]
left_array = [x for x in a if x < a[0]]
left_array = [x for x in a[1:] if x <= a[0]]
print(left_array)
right_array = [x for x in a if x > a[0]]
right_array = [x for x in a[1:] if x > a[0]]
print(right_array)
return a
Expand All @@ -59,11 +59,11 @@ quicksort(a)
# [3, 2, 1, 5, 4]
```

然后第一轮,我们就可以组装成新的数组,这个数组中间的元素是a[0],左边都比它小,右边都比它大。
然后第一轮,我们就可以组装成新的数组,这个数组中间的元素是a[0],左边都比它小,右边都比它大。注意这里是a[1:]而不是a。

```
def quicksort(a):
return [x for x in a if x < a[0]] + [a[0]] + [x for x in a if x > a[0]]
return [x for x in a[1:] if x < a[0]] + [a[0]] + [x for x in a[1:] if x > a[0]]
quicksort(a)
# Print [2, 1, 3, 5, 4]
Expand All @@ -76,7 +76,7 @@ def quicksort(a):
if len(a) <=1:
return a
else:
return quicksort([x for x in a if x < a[0]]) + [a[0]] + quicksort([x for x in a if x > a[0]])
return quicksort([x for x in a[1:] if x < a[0]]) + [a[0]] + quicksort([x for x in a[1:] if x > a[0]])
quicksort(a)
# Print [1, 2, 3, 4, 5]
Expand Down
2 changes: 1 addition & 1 deletion 003树.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ tree.add_node(tree.root, 20)
tree.pre_travel(tree.root)
# Print [5, 10, 15, 20]
```

这是本章内容,希望对你有所帮助。[进入下一章](./004二分查询.md)
Empty file added 005链表.md
Empty file.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

# Tobe Algorithm Manual

* [开始学习](./001快速排序.md)
通过Python记录各种算法的实现原理,由浅入深一步步分析编码步骤。

[开始学习](./001快速排序.md)

0 comments on commit 6f5d7c7

Please sign in to comment.