设计一个程序,比较冒泡排序、选择排序、插入排序、shell排序、快速排序、堆排序、归并排序等排序算法的性能。
时间: 2023-09-24 08:06:01 浏览: 73
以下是一个Python程序,可以用来比较不同排序算法的性能:
```python
import random
import time
# 冒泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, 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 insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
# 希尔排序
def shell_sort(arr):
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
# 快速排序
def quick_sort(arr):
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]
return quick_sort(left) + middle + quick_sort(right)
# 堆排序
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[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
# 归并排序
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr)//2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
# 生成随机数列
def generate_random_array(n):
arr = []
for i in range(n):
arr.append(random.randint(1, 1000))
return arr
# 生成有序数列
def generate_sorted_array(n):
arr = []
for i in range(n):
arr.append(i+1)
return arr
# 生成倒序数列
def generate_reversed_array(n):
arr = []
for i in range(n, 0, -1):
arr.append(i)
return arr
# 比较不同排序算法的性能
def compare_sorting_algorithms():
n = 10000
random_array = generate_random_array(n)
sorted_array = generate_sorted_array(n)
reversed_array = generate_reversed_array(n)
# 冒泡排序
start_time = time.time()
bubble_sort(random_array[:])
end_time = time.time()
print("Bubble sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
bubble_sort(sorted_array[:])
end_time = time.time()
print("Bubble sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
bubble_sort(reversed_array[:])
end_time = time.time()
print("Bubble sort (reversed array): ", end_time - start_time, "seconds")
# 选择排序
start_time = time.time()
selection_sort(random_array[:])
end_time = time.time()
print("Selection sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
selection_sort(sorted_array[:])
end_time = time.time()
print("Selection sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
selection_sort(reversed_array[:])
end_time = time.time()
print("Selection sort (reversed array): ", end_time - start_time, "seconds")
# 插入排序
start_time = time.time()
insertion_sort(random_array[:])
end_time = time.time()
print("Insertion sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
insertion_sort(sorted_array[:])
end_time = time.time()
print("Insertion sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
insertion_sort(reversed_array[:])
end_time = time.time()
print("Insertion sort (reversed array): ", end_time - start_time, "seconds")
# 希尔排序
start_time = time.time()
shell_sort(random_array[:])
end_time = time.time()
print("Shell sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
shell_sort(sorted_array[:])
end_time = time.time()
print("Shell sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
shell_sort(reversed_array[:])
end_time = time.time()
print("Shell sort (reversed array): ", end_time - start_time, "seconds")
# 快速排序
start_time = time.time()
quick_sort(random_array[:])
end_time = time.time()
print("Quick sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
quick_sort(sorted_array[:])
end_time = time.time()
print("Quick sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
quick_sort(reversed_array[:])
end_time = time.time()
print("Quick sort (reversed array): ", end_time - start_time, "seconds")
# 堆排序
start_time = time.time()
heap_sort(random_array[:])
end_time = time.time()
print("Heap sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
heap_sort(sorted_array[:])
end_time = time.time()
print("Heap sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
heap_sort(reversed_array[:])
end_time = time.time()
print("Heap sort (reversed array): ", end_time - start_time, "seconds")
# 归并排序
start_time = time.time()
merge_sort(random_array[:])
end_time = time.time()
print("Merge sort (random array): ", end_time - start_time, "seconds")
start_time = time.time()
merge_sort(sorted_array[:])
end_time = time.time()
print("Merge sort (sorted array): ", end_time - start_time, "seconds")
start_time = time.time()
merge_sort(reversed_array[:])
end_time = time.time()
print("Merge sort (reversed array): ", end_time - start_time, "seconds")
# 调用函数
compare_sorting_algorithms()
```
这个程序使用了Python实现了冒泡排序、选择排序、插入排序、希尔排序、快速排序、堆排序和归并排序。它还生成了三种不同类型的数列(随机数列、有序数列和倒序数列),并对每种排序算法在这些数列上的性能进行了测试。
注意:由于不同机器的性能不同,因此这个程序的运行时间可能会有所不同。
阅读全文