1.排序算法比较 利用随机函数产生30000个随机整数,利用插入排序、起泡排序、选择排序、快速排序、堆排序、归并排序等排序方法进行排序,并且 (1)统计每一种排序上机所花费的时间。 (2)统计在完全正序,完全逆序情况下记录的比较次数和移动次数。 (3)比较的指标为关键字的比较次数和记录的移动次数(一次记录交换计为3次移动)。 (4)对结果作简单分析,包括对各组数据得出结果波动大小的解释。
时间: 2024-02-16 20:03:24 浏览: 101
本题需要编写程序进行排序算法比较,下面给出一个 Python 代码示例:
```python
import random
import time
# 插入排序
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
# 起泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
# 选择排序
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
# 快速排序
def quick_sort(arr):
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i+1], arr[high] = arr[high], arr[i+1]
return i+1
def sort_helper(arr, low, high):
if low < high:
pivot_idx = partition(arr, low, high)
sort_helper(arr, low, pivot_idx-1)
sort_helper(arr, pivot_idx+1, high)
sort_helper(arr, 0, len(arr)-1)
# 堆排序
def heap_sort(arr):
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[l] > arr[largest]:
largest = l
if r < n and arr[r] > arr[largest]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
n = len(arr)
for i in range(n//2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)
# 归并排序
def merge_sort(arr):
def merge(arr, l, m, r):
n1 = m - l + 1
n2 = r - m
L = [0] * n1
R = [0] * n2
for i in range(n1):
L[i] = arr[l+i]
for i in range(n2):
R[i] = arr[m+1+i]
i = 0
j = 0
k = l
while i < n1 and j < n2:
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
def sort_helper(arr, l, r):
if l < r:
m = (l + r) // 2
sort_helper(arr, l, m)
sort_helper(arr, m+1, r)
merge(arr, l, m, r)
sort_helper(arr, 0, len(arr)-1)
# 生成随机数
arr = [random.randint(0, 100000) for i in range(30000)]
# 插入排序
start_time = time.time()
insertion_sort(arr.copy())
end_time = time.time()
insertion_time = end_time - start_time
# 起泡排序
start_time = time.time()
bubble_sort(arr.copy())
end_time = time.time()
bubble_time = end_time - start_time
# 选择排序
start_time = time.time()
selection_sort(arr.copy())
end_time = time.time()
selection_time = end_time - start_time
# 快速排序
start_time = time.time()
quick_sort(arr.copy())
end_time = time.time()
quick_time = end_time - start_time
# 堆排序
start_time = time.time()
heap_sort(arr.copy())
end_time = time.time()
heap_time = end_time - start_time
# 归并排序
start_time = time.time()
merge_sort(arr.copy())
end_time = time.time()
merge_time = end_time - start_time
print("插入排序时间:", insertion_time)
print("起泡排序时间:", bubble_time)
print("选择排序时间:", selection_time)
print("快速排序时间:", quick_time)
print("堆排序时间:", heap_time)
print("归并排序时间:", merge_time)
```
这段代码生成了 30000 个随机整数,并分别使用了插入排序、起泡排序、选择排序、快速排序、堆排序、归并排序进行排序,并统计了每一种排序算法所花费的时间。同时,如果需要统计完全正序或完全逆序情况下的比较次数和移动次数,可以在每个排序算法中添加相关的计数代码。波动大小的解释可以通过对比不同排序算法的时间复杂度和算法特点来给出。
阅读全文