bubble sort show me
时间: 2023-11-20 21:02:18 浏览: 192
bubble sort
Sure! Here's a simple implementation of bubble sort in Python:
```
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
# Example usage:
my_list = [3, 7, 1, 9, 4]
sorted_list = bubble_sort(my_list)
print(sorted_list)
```
This code uses two nested loops to compare adjacent elements in the array and swap them if they're out of order. The sorting process continues until no more swaps are needed, indicating that the array is now fully sorted.
阅读全文