Write a bubble sort in python
时间: 2023-05-22 14:05:28 浏览: 156
Sure, here's an example code for bubble sort in Python:
```
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# Traverse the array from 0 to n-i-1
# Swap if the element found is greater than the next element
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
```
To use this function, just pass in a list of numbers you want to sort as the argument.
阅读全文