数组排序sort从小到大
时间: 2024-01-11 13:20:50 浏览: 76
深入理解js数组的sort排序
以下是使用选择排序算法对数组进行从小到大排序的示例:
```python
def findSmallest(arr):
smallest = arr[0]
smallest_index = 0
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest_index = findSmallest(arr)
newArr.append(arr.pop(smallest_index))
return newArr
arr = [64, 25, 12, 22, 11]
sorted_arr = selectionSort(arr)
print("Sorted array:", sorted_arr) # 输出:[11, 12, 22, 25, 64]
```
阅读全文