3.排序算法的多功能演示系统问题描述:设计和实现内部排序算法效率比较平台,通过随机数函数生成无序的数据动态演示各排序算法的排序过程和计算效率。具体要求:详细比较“直接插入、希尔、直接选择、堆、冒泡、快速、归并,基数排序”等排序算法。完成两个功能:1)通过随机数函数生成15个(0-99)的整数,在系统里通过选择不同的排序算法进行演示并显示每一趟的排序结果,同时给出排序算法的关键字比较次数和移动次数;2)通过随机数函数生成100000个(0-9999)的整数,同时输出各排序算法的具体计算时间。
时间: 2024-03-12 18:43:41 浏览: 71
排序算法演示:插入排序、选择排序、冒泡排序、希尔排序、归并排序、快速排序
对于这个问题,我可以给出一个简单实现的代码示例,供参考。以下是Python代码:
```python
import random
import time
# 直接插入排序
def insertion_sort(arr):
n = len(arr)
comparisons = 0
exchanges = 0
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
comparisons += 1
arr[j + 1] = arr[j]
j -= 1
exchanges += 1
arr[j + 1] = key
exchanges += 1
return comparisons, exchanges
# 希尔排序
def shell_sort(arr):
n = len(arr)
comparisons = 0
exchanges = 0
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:
comparisons += 1
arr[j] = arr[j - gap]
j -= gap
exchanges += 1
arr[j] = temp
exchanges += 1
gap //= 2
return comparisons, exchanges
# 直接选择排序
def selection_sort(arr):
n = len(arr)
comparisons = 0
exchanges = 0
for i in range(n - 1):
min_idx = i
for j in range(i + 1, n):
comparisons += 1
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
exchanges += 1
return comparisons, exchanges
# 堆排序
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]:
comparisons += 1
largest = l
if r < n and arr[r] > arr[largest]:
comparisons += 1
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
exchanges += 1
heapify(arr, n, largest)
n = len(arr)
comparisons = 0
exchanges = 0
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
exchanges += 1
heapify(arr, i, 0)
return comparisons, exchanges
# 冒泡排序
def bubble_sort(arr):
n = len(arr)
comparisons = 0
exchanges = 0
for i in range(n):
for j in range(n - i - 1):
comparisons += 1
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
exchanges += 1
return comparisons, exchanges
# 快速排序
def quick_sort(arr):
def partition(arr, low, high):
i = low - 1
pivot = arr[high]
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
exchanges += 1
comparisons += 1
arr[i + 1], arr[high] = arr[high], arr[i + 1]
exchanges += 1
return i + 1
def quick_sort_helper(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort_helper(arr, low, pi - 1)
quick_sort_helper(arr, pi + 1, high)
n = len(arr)
comparisons = 0
exchanges = 0
quick_sort_helper(arr, 0, n - 1)
return comparisons, exchanges
# 归并排序
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
comparisons = 0
exchanges = 0
while i < n1 and j < n2:
comparisons += 1
if L[i] <= R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
exchanges += 1
while i < n1:
arr[k] = L[i]
i += 1
k += 1
exchanges += 1
while j < n2:
arr[k] = R[j]
j += 1
k += 1
exchanges += 1
return comparisons, exchanges
def merge_sort_helper(arr, l, r):
comparisons = 0
exchanges = 0
if l < r:
m = (l + r) // 2
comparisons_l, exchanges_l = merge_sort_helper(arr, l, m)
comparisons += comparisons_l
exchanges += exchanges_l
comparisons_r, exchanges_r = merge_sort_helper(arr, m + 1, r)
comparisons += comparisons_r
exchanges += exchanges_r
comparisons_merge, exchanges_merge = merge(arr, l, m, r)
comparisons += comparisons_merge
exchanges += exchanges_merge
else:
comparisons = 0
exchanges = 0
return comparisons, exchanges
n = len(arr)
return merge_sort_helper(arr, 0, n - 1)
# 基数排序
def radix_sort(arr):
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
idx = (arr[i] // exp) % 10
count[idx] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = n - 1
while i >= 0:
idx = (arr[i] // exp) % 10
output[count[idx] - 1] = arr[i]
count[idx] -= 1
i -= 1
i = 0
for i in range(n):
arr[i] = output[i]
return n - 1, n - 1
max_val = max(arr)
exp = 1
comparisons = 0
exchanges = 0
while max_val // exp > 0:
comparisons_round, exchanges_round = counting_sort(arr, exp)
comparisons += comparisons_round
exchanges += exchanges_round
exp *= 10
return comparisons, exchanges
# 生成随机数
def generate_random_data(n):
return [random.randint(0, 99) for _ in range(n)]
# 生成100000个随机数
data = [random.randint(0, 9999) for _ in range(100000)]
# 选择排序算法进行演示
algorithms = [
("直接插入排序", insertion_sort),
("希尔排序", shell_sort),
("直接选择排序", selection_sort),
("堆排序", heap_sort),
("冒泡排序", bubble_sort),
("快速排序", quick_sort),
("归并排序", merge_sort),
("基数排序", radix_sort)
]
print("排序算法效率比较平台")
print("======================")
# 功能1:通过随机数函数生成15个(0-99)的整数,在系统里通过选择不同的排序算法进行演示并显示每一趟的排序结果,同时给出排序算法的关键字比较次数和移动次数
print("演示排序算法")
print("----------------------")
data_15 = generate_random_data(15)
for name, algorithm in algorithms:
arr = data_15.copy()
comparisons, exchanges = algorithm(arr)
print(name)
print("排序前:", data_15)
print("排序后:", arr)
print("关键字比较次数:", comparisons)
print("移动次数:", exchanges)
print("----------------------")
# 功能2:通过随机数函数生成100000个(0-9999)的整数,同时输出各排序算法的具体计算时间
print("计算排序算法时间")
print("----------------------")
for name, algorithm in algorithms:
arr = data.copy()
start_time = time.time()
algorithm(arr)
end_time = time.time()
print(name)
print("计算时间:", end_time - start_time, "秒")
print("----------------------")
```
以上代码实现了各种排序算法,并演示了如何使用这些算法进行排序,并计算出每个算法的关键字比较次数和移动次数。同时,还演示了如何使用这些算法对100000个随机数进行排序,并输出它们的具体计算时间。需要注意的是,这只是一个简单的示例,实际上还有很多需要考虑的细节和问题,例如算法的优化、内存的管理等等。
阅读全文