bubble sorting code
时间: 2023-10-15 13:04:28 浏览: 43
Here is an example of bubble sort code in Python:
```
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
```
This code defines a function `bubble_sort` that takes an array `arr` as input and returns the sorted array using the bubble sort algorithm. The function first determines the length of the array `n`. It then iterates over the array `n` times using a nested loop, comparing adjacent elements and swapping them if they are in the wrong order. The largest element "bubbles up" to the end of the array on each iteration, so the inner loop only needs to go up to `n-i-1` on the `i`th iteration. After all iterations, the sorted array is returned.