Skip to content

Commit

Permalink
添加了选择排序和冒泡排序
Browse files Browse the repository at this point in the history
  • Loading branch information
luojunll committed Jan 14, 2025
1 parent 9c7e9bc commit 19f35e6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/code/算法/选择排序、冒泡排序.md
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))
```

0 comments on commit 19f35e6

Please sign in to comment.