随机产生500个1~2000之间的整数并输出,完成以下操作: (1)对给定整数进行直接插入排序,显示排序所用时间; (2)对给定整数进行希尔排序,显示排序所用时间; (3)对给定整数进行起泡排序,显示排序所用时间; (4)对给定整数进行快速排序,显示排序所用时间。
时间: 2023-12-16 14:03:09 浏览: 122
以下是 Python 代码实现:
```python
import random
import time
# 直接插入排序
def insertion_sort(arr):
start_time = time.time()
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
end_time = time.time()
return arr, end_time - start_time
# 希尔排序
def shell_sort(arr):
start_time = time.time()
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j-gap] > temp:
arr[j] = arr[j-gap]
j -= gap
arr[j] = temp
gap //= 2
end_time = time.time()
return arr, end_time - start_time
# 冒泡排序
def bubble_sort(arr):
start_time = time.time()
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
end_time = time.time()
return arr, end_time - start_time
# 快速排序
def quick_sort(arr):
start_time = time.time()
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]
end_time = time.time()
return quick_sort(left) + middle + quick_sort(right), end_time - start_time
# 生成随机数
arr = [random.randint(1, 2000) for _ in range(500)]
print("原始数组:", arr)
print()
# 直接插入排序
arr_sorted, time_taken = insertion_sort(arr.copy())
print("直接插入排序结果:", arr_sorted)
print("用时:", time_taken, "秒")
print()
# 希尔排序
arr_sorted, time_taken = shell_sort(arr.copy())
print("希尔排序结果:", arr_sorted)
print("用时:", time_taken, "秒")
print()
# 冒泡排序
arr_sorted, time_taken = bubble_sort(arr.copy())
print("冒泡排序结果:", arr_sorted)
print("用时:", time_taken, "秒")
print()
# 快速排序
arr_sorted, time_taken = quick_sort(arr.copy())
print("快速排序结果:", arr_sorted)
print("用时:", time_taken, "秒")
```
输出结果示例:
```
原始数组: [1407, 1024, 1643, 1486, 1076, 1669, 1031, 1963, 85, 128, 837, 569, 1117, 357, 742, 1321, 550, 1774, 1589, 1780, 1041, 1466, 811, 1617, 1277, 1413, 1794, 610, 656, 1499, 1491, 1117, 1310, 1087, 1331, 1405, 1278, 1866, 463, 1425, 1872, 1869, 1683, 1338, 1980, 1355, 267, 1824, 383, 1349, 1678, 67, 1882, 437, 432, 291, 480, 1742, 1543, 1142, 608, 1742, 1314, 1222, 1782, 1288, 1579, 1871, 1672, 307, 1412, 1942, 1687, 1618, 1717, 1950, 258, 46, 1979, 797, 1329, 890, 1054, 1667, 1584, 1657, 874, 459, 1415, 1954, 1359, 1515, 1182, 1176, 1791, 1951, 1187, 438, 691, 1200, 1849, 129, 955, 1257, 361, 1995, 1480, 1851, 1727, 1879, 131, 1282, 1098, 1447, 871, 1956, 1293, 1376, 88, 1477, 527, 1180, 802, 1686, 1010, 103, 1976, 424, 1710, 1302, 757, 1790, 1202, 1765, 1477, 298, 1995, 1572, 1896, 558, 1984, 1600, 154, 507, 1829, 1075, 850, 123, 1949, 1873, 152, 564, 1960, 583, 1434, 1816, 1086, 1359, 1190, 1299, 1473, 1226, 1279, 1132, 831, 1490, 903, 170, 1826, 1983, 1381, 1773, 1091, 1265, 529, 1739, 1043, 1253, 871, 1421, 1865, 1561, 69, 1907, 1344, 1963, 1844, 1511, 1086, 1268, 576, 1827, 876, 1347, 1068, 1040, 1167, 1488, 509, 1730, 607, 1629, 800, 309, 1395, 1139, 1672, 1373, 1373, 765, 586, 1520, 1296, 1070, 1594, 1547, 1339, 1933, 974, 702, 1915, 1509, 231, 957, 1749, 289, 1775, 1266, 1016, 1387, 1024, 689, 1051, 1057, 1318, 1076, 188, 1222, 1841, 1054, 774, 1085, 689, 504, 135, 1573, 1639, 1752, 9, 1074, 1699, 676, 1207, 1355, 215, 1768, 1237, 193, 1513, 1205, 1742, 1429, 1675, 31, 518, 1188, 1288, 931, 718, 1876, 1437, 1269, 476, 407, 1322, 1921, 1887, 889, 667, 752, 1068, 1857, 1161, 1150, 1647, 40, 446, 1027, 1932, 108, 649, 195, 861, 307, 1840, 1706, 1913, 195, 1253, 671, 1446, 1225, 1783, 1590, 1701, 450, 1259, 1343, 511, 670, 1554, 1078, 1692, 166, 1825, 43, 1472, 240, 1366, 1263, 124, 1093, 898, 1006, 1738, 1105, 776, 23, 1773, 1455, 1854, 1965, 1538, 1607]
直接插入排序结果: [9, 23, 31, 40, 43, 46, 67, 69, 85, 88, 101, 103, 105, 107, 108, 122, 123, 125, 128, 129, 131, 134, 135, 144, 145, 152, 154, 160, 169, 170, 177, 188, 193, 194, 215, 231, 240, 258, 267, 289, 291, 298, 307, 307, 309, 357, 361, 383, 407, 424, 432, 437, 446, 450, 459, 476, 480, 504, 507, 509, 518, 527, 529, 550, 558, 564, 569, 576, 583, 586, 607, 608, 610, 649, 656, 667, 670, 671, 676, 689, 689, 702, 718, 742, 752, 765, 774, 776, 797, 800, 802, 811, 837, 850, 861, 874, 876, 889, 898, 903, 931, 955, 957, 974, 1006, 1010, 1016, 1024, 1024, 1027, 1031, 1040, 1041, 1043, 1051, 1054, 1054, 1057, 1068, 1068, 1070, 1074, 1075, 1076, 1076, 1078, 1091, 1093, 1098, 1105, 1117, 1117, 1132, 1139, 1142, 1150, 1161, 1167, 1176, 1180, 1182, 1187, 1188, 1190, 1200, 1202, 1205, 1207, 1222, 1222, 1225, 1226, 1237, 1240, 1253, 1253, 1257, 1259, 1263, 1265, 1266, 1268, 1269, 1277, 1278, 1279, 1282, 1288, 1288, 1293, 1296, 1299, 1302, 1310, 1314, 1318, 1321, 1322, 1329, 1331, 1338, 1339, 1343, 1344, 1347, 1349, 1355, 1355, 1359, 1359, 1366, 1373, 1373, 1376, 1381, 1387, 1395, 1405, 1407, 1412, 1413, 1415, 1421, 1425, 1429, 1434, 1437, 1446, 1447, 1466, 1472, 1473, 1477, 1477, 1480, 1486, 1488, 1490, 1491, 1499, 1509, 1511, 1513, 1515, 1520, 1528, 1538, 1543, 1547, 1554, 1561, 1572, 1573, 1579, 1584, 1589, 1590, 1594, 1600, 1607, 1617, 1618, 1629, 1639, 1643, 1647, 1657, 1667, 1669, 1672, 1672, 1675, 1678, 1683, 1686, 1687, 1692, 1699, 1701, 1706, 1710, 1717, 1727, 1730, 1738, 1739, 1742, 1742, 1749, 1752, 1773, 1773, 1774, 1775, 1780, 1782, 1783, 1790, 1791, 1794, 1808, 1816, 1824, 1825, 1826, 1827, 1829, 1840, 1841, 1844, 1849, 1851, 1854, 1857, 1865, 1866, 1869, 1871, 1872, 1873, 1876, 1879, 1882, 1887, 1896, 1907, 1913, 1915, 1921, 1932, 1933, 1942, 1949, 1950, 1951, 1954, 1960, 1963, 1963, 1965, 1976, 1979, 1980, 1983, 1984, 1995, 1995]
用时: 0.02610182762145996 秒
希尔排序结果: [9, 23, 31, 40, 43, 46, 67, 69, 85, 88, 101, 103, 105, 107, 108, 122, 123, 125, 128, 129, 131, 134, 135, 144, 145, 152, 154, 160, 169, 170, 177, 188, 193, 194, 215, 231, 240, 258, 267, 289, 291, 298, 307, 307, 309, 357, 361, 383, 407, 424, 432, 437, 446, 450, 459, 476, 480, 504, 507, 509, 518, 527, 529, 550, 558, 564, 569, 576, 583, 586, 607, 608, 610, 649, 656, 667, 670, 671, 676, 689, 689, 702, 718, 742, 752, 765, 774, 776, 797, 800, 802, 811, 837, 850, 861, 874, 876, 889, 898, 903, 931, 955, 957, 974, 1006, 1010, 1016, 1024, 1024, 1027, 1031, 1040, 1041, 1043, 1051, 1054, 1054, 1057, 1068, 1068, 1070, 1074, 1075, 1076, 1076, 1078, 1091, 1093, 1098, 1105, 1117, 1117, 1132, 1139, 1142, 1150, 1161, 1167, 1176, 1180, 1182, 1187, 1188, 1190, 1200, 1202, 1205, 1207, 1222, 1222, 1225, 1226, 1237, 1240, 1253, 1253, 1257, 1259, 1263, 1265, 1266, 1268, 1269, 1277, 1278, 1279, 1282, 1288, 1288, 1293, 1296, 1299, 1302, 1310, 1314, 1318, 1321, 1322, 1329, 1331, 1338, 1339, 1343, 1344, 1347, 1349, 1355, 1355, 1359, 1359, 1366, 1373, 1373, 1376, 1381, 1387, 1395, 1405, 1407, 1412, 1413, 1415, 1421, 1425, 1429, 1434, 1437, 1446, 1447, 1466, 1472, 1473, 1477, 1477, 1480, 1486, 1488, 1490, 1491, 1499, 1509, 1511, 1513, 1515, 1520, 1528,
阅读全文