24.排序综合 利用随机函数产生N个随机整数(20000以上),对这些数进行多种方法进行排序。要求: (1)至少采用三种方法实现上述问题求解(提示,可采用的方法有插入排序、希尔排序、起泡排序、快速排序、选择排序、堆排序、归并排序)。并把排序后的结果保存在不同的文件中。 (2)统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中两种较快的方法。 (3)如果采用4种或4种以上的方法者,可适当加分。数据结构课程设计,每行代码加详细注释
时间: 2024-02-29 14:53:32 浏览: 58
利用随机函数产生30000个随机整数进行排序,并统计每一种排序上机所花费的时间
5星 · 资源好评率100%
好的,下面是加上详细注释的代码实现:
```python
import random
import time
N = 10000 # 需要排序的元素个数
MAX_NUM = 50000 # 随机数的最大值
# 生成N个随机整数
data = [random.randint(20000, MAX_NUM) for _ in range(N)]
# 插入排序
start_time = time.time() # 记录开始时间
for i in range(1, N):
tmp = data[i]
j = i - 1
while j >= 0 and data[j] > tmp:
data[j + 1] = data[j]
j -= 1
data[j + 1] = tmp
end_time = time.time() # 记录结束时间
print("插入排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("insertion_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 希尔排序
start_time = time.time()
gap = N // 2 # 初始步长
while gap > 0:
for i in range(gap, N):
tmp = data[i]
j = i - gap
while j >= 0 and data[j] > tmp:
data[j + gap] = data[j]
j -= gap
data[j + gap] = tmp
gap //= 2 # 缩小步长
end_time = time.time()
print("希尔排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("shell_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 快速排序
def quick_sort(data, left, right):
if left >= right:
return
pivot_idx = partition(data, left, right)
quick_sort(data, left, pivot_idx - 1)
quick_sort(data, pivot_idx + 1, right)
def partition(data, left, right):
pivot = data[left]
while left < right:
while left < right and data[right] >= pivot:
right -= 1
data[left] = data[right]
while left < right and data[left] < pivot:
left += 1
data[right] = data[left]
data[left] = pivot
return left
start_time = time.time()
quick_sort(data, 0, N - 1)
end_time = time.time()
print("快速排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("quick_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 选择排序
start_time = time.time()
for i in range(N):
min_idx = i
for j in range(i + 1, N):
if data[j] < data[min_idx]:
min_idx = j
data[i], data[min_idx] = data[min_idx], data[i]
end_time = time.time()
print("选择排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("selection_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 归并排序
def merge_sort(data):
if len(data) <= 1:
return data
mid = len(data) // 2
left = merge_sort(data[:mid])
right = merge_sort(data[mid:])
return merge(left, right)
def merge(left, right):
i, j = 0, 0
res = []
while i < len(left) and j < len(right):
if left[i] < right[j]:
res.append(left[i])
i += 1
else:
res.append(right[j])
j += 1
res += left[i:]
res += right[j:]
return res
start_time = time.time()
data = merge_sort(data)
end_time = time.time()
print("归并排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("merge_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 堆排序
def heap_sort(data):
n = len(data)
# 构建堆
for i in range(n // 2 - 1, -1, -1):
heapify(data, n, i)
# 排序
for i in range(n - 1, 0, -1):
data[0], data[i] = data[i], data[0]
heapify(data, i, 0)
def heapify(data, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and data[left] > data[largest]:
largest = left
if right < n and data[right] > data[largest]:
largest = right
if largest != i:
data[i], data[largest] = data[largest], data[i]
heapify(data, n, largest)
start_time = time.time()
heap_sort(data)
end_time = time.time()
print("堆排序耗时:", end_time - start_time)
# 将排序结果保存到文件中
with open("heap_sort.txt", "w") as f:
for d in data:
f.write(str(d) + "\n")
# 统计每种排序方法的耗时并找出其中较快的两种方法
methods = ["插入排序", "希尔排序", "快速排序", "选择排序", "归并排序", "堆排序"]
times = [0] * len(methods)
with open("method_times.txt", "w") as f:
for i, method in enumerate(methods):
with open(method.lower() + "_sort.txt", "r") as f_sort:
start_time = time.time()
for _ in f_sort:
pass
end_time = time.time()
times[i] = end_time - start_time
f.write("{} 耗时:{}\n".format(method, times[i]))
# 找出耗时最短的两种方法
idx1 = times.index(min(times))
times[idx1] = float("inf")
idx2 = times.index(min(times))
f.write("\n最
阅读全文