generated from rockbenben/LearnData
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 选择排序 | ||
```python | ||
""" | ||
选择排序,从未排序序列中选择一个最小的值放到未排序序列首位 | ||
""" | ||
def select(lst): | ||
for i in range(len(lst)-1): | ||
k = i | ||
for j in range(i+1, len(lst)): | ||
if lst[j] < lst[k]: | ||
k = j | ||
lst[i], lst[k] = lst[k], lst[i] | ||
return lst | ||
|
||
lst = [4, 1, 3, 1, 5, 2, 7, 6] | ||
print(select(lst)) | ||
``` | ||
|
||
# 冒泡排序 | ||
|
||
```python | ||
""" | ||
冒泡排序, 比较相邻位置元素的大小, 把大的往后排, 最后会把最大的元素排到最后, 像冒泡一样。 | ||
""" | ||
def buble(lst): | ||
for i in range(len(lst)- 1, 0, -1): | ||
for j in range(i): | ||
if lst[j] > lst[j+1]: | ||
lst[j], lst[j+1] = lst[j+1], lst[j] | ||
return lst | ||
|
||
lst = [4, 1, 3, 1, 5, 2, 7, 6] | ||
print(buble(lst)) | ||
``` |