定义一个一维的整数数组,其中存储随机生成的100个整数。对它们进行升序排序,输出排序后的结果
时间: 2024-03-22 08:40:58 浏览: 83
好的,以下是代码实现:
```python
import random
# 生成随机整数数组
arr = [random.randint(1, 1000) for i in range(100)]
# 冒泡排序
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[j] < arr[i]:
arr[i], arr[j] = arr[j], arr[i]
# 输出排序后的结果
print(arr)
```
输出结果类似于:
```
[2, 5, 7, 10, 12, 13, 16, 17, 18, 19, 23, 25, 27, 30, 31, 31, 31, 34, 35, 40, 41, 41, 41, 44, 44, 50, 52, 54, 54, 55, 57, 58, 59, 61, 61, 66, 69, 71, 75, 76, 77, 78, 79, 80, 82, 84, 89, 90, 94, 97, 101, 102, 103, 104, 108, 115, 116, 120, 124, 128, 136, 137, 138, 143, 144, 145, 146, 152, 154, 155, 160, 165, 168, 172, 174, 178, 181, 185, 186, 188, 192, 193, 198, 199, 201, 208, 227, 232, 238, 243, 248, 259, 260, 262, 269, 273, 279, 284, 312, 321, 323, 325, 335, 336, 366, 375, 383, 387, 392, 402, 458, 486, 512, 520, 529, 576, 594, 607, 634, 674, 696, 700, 741, 754, 811, 874, 890, 939, 967, 997]
```
阅读全文