写个python排序代码
时间: 2023-10-14 09:24:22 浏览: 57
以下是一个快速排序算法的Python实现:
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
```
使用方法:
```python
arr = [3, 6, 1, 8, 4, 2, 9, 5, 7]
sorted_arr = quick_sort(arr)
print(sorted_arr)
```
输出:
```
[1, 2, 3, 4, 5, 6, 7, 8, 9]
```
阅读全文