利用随机函数产生N个随机整数(20000以上),对这些数进行多种方法进行排序。要求:(1)至少采用三种方法(冒泡排序、快速排序、合并排序)实现上述问题求解;(2)统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出其中两种较快的方法;(3)统计每种算法所用的比较次数和交换次数,最后列表显示;
时间: 2023-12-22 21:04:53 浏览: 96
利用随机函数产生30000个随机整数进行排序,并统计每一种排序上机所花费的时间
5星 · 资源好评率100%
以下是Python代码实现:
```python
import random
import time
# 冒泡排序
def bubble_sort(nums):
n = len(nums)
for i in range(n):
for j in range(n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
# 快速排序
def quick_sort(nums):
_quick_sort(nums, 0, len(nums)-1)
def _quick_sort(nums, left, right):
if left < right:
pivot_index = partition(nums, left, right)
_quick_sort(nums, left, pivot_index-1)
_quick_sort(nums, pivot_index+1, right)
def partition(nums, left, right):
pivot = nums[left]
while left < right:
while left < right and nums[right] >= pivot:
right -= 1
nums[left] = nums[right]
while left < right and nums[left] <= pivot:
left += 1
nums[right] = nums[left]
nums[left] = pivot
return left
# 合并排序
def merge_sort(nums):
if len(nums) <= 1:
return nums
mid = len(nums) // 2
left_nums = merge_sort(nums[:mid])
right_nums = merge_sort(nums[mid:])
return merge(left_nums, right_nums)
def merge(left_nums, right_nums):
result = []
i = j = 0
while i < len(left_nums) and j < len(right_nums):
if left_nums[i] <= right_nums[j]:
result.append(left_nums[i])
i += 1
else:
result.append(right_nums[j])
j += 1
result += left_nums[i:]
result += right_nums[j:]
return result
# 产生N个随机数
N = 10000
nums = [random.randint(20000, 99999) for _ in range(N)]
# 冒泡排序性能测试
start_time = time.time()
bubble_sort(nums.copy())
end_time = time.time()
bubble_sort_time = end_time - start_time
# 快速排序性能测试
start_time = time.time()
quick_sort(nums.copy())
end_time = time.time()
quick_sort_time = end_time - start_time
# 合并排序性能测试
start_time = time.time()
merge_sort(nums.copy())
end_time = time.time()
merge_sort_time = end_time - start_time
print(f"冒泡排序耗时:{bubble_sort_time:.4f}s")
print(f"快速排序耗时:{quick_sort_time:.4f}s")
print(f"合并排序耗时:{merge_sort_time:.4f}s")
# 比较次数和交换次数统计
def bubble_sort_count(nums):
n = len(nums)
compare_count = exchange_count = 0
for i in range(n):
for j in range(n-i-1):
compare_count += 1
if nums[j] > nums[j+1]:
exchange_count += 1
nums[j], nums[j+1] = nums[j+1], nums[j]
return compare_count, exchange_count
def quick_sort_count(nums):
compare_count = exchange_count = 0
_quick_sort_count(nums, 0, len(nums)-1, compare_count, exchange_count)
return compare_count, exchange_count
def _quick_sort_count(nums, left, right, compare_count, exchange_count):
if left < right:
pivot_index, compare_count, exchange_count = partition_count(nums, left, right, compare_count, exchange_count)
_quick_sort_count(nums, left, pivot_index-1, compare_count, exchange_count)
_quick_sort_count(nums, pivot_index+1, right, compare_count, exchange_count)
def partition_count(nums, left, right, compare_count, exchange_count):
pivot = nums[left]
while left < right:
while left < right and nums[right] >= pivot:
compare_count += 1
right -= 1
exchange_count += 1
nums[left] = nums[right]
while left < right and nums[left] <= pivot:
compare_count += 1
left += 1
exchange_count += 1
nums[right] = nums[left]
nums[left] = pivot
return left, compare_count, exchange_count
def merge_sort_count(nums):
if len(nums) <= 1:
return nums, 0, 0
mid = len(nums) // 2
left_nums, compare_count1, exchange_count1 = merge_sort_count(nums[:mid])
right_nums, compare_count2, exchange_count2 = merge_sort_count(nums[mid:])
result = []
i = j = 0
compare_count = compare_count1 + compare_count2
exchange_count = exchange_count1 + exchange_count2
while i < len(left_nums) and j < len(right_nums):
compare_count += 1
if left_nums[i] <= right_nums[j]:
result.append(left_nums[i])
i += 1
else:
result.append(right_nums[j])
j += 1
result += left_nums[i:]
result += right_nums[j:]
exchange_count += len(result) - len(nums)
return result, compare_count, exchange_count
# 统计比较次数和交换次数
bubble_sort_compare_count, bubble_sort_exchange_count = bubble_sort_count(nums.copy())
quick_sort_compare_count, quick_sort_exchange_count = quick_sort_count(nums.copy())
merge_sort_nums, merge_sort_compare_count, merge_sort_exchange_count = merge_sort_count(nums.copy())
# 结果输出
print("冒泡排序比较次数:", bubble_sort_compare_count)
print("冒泡排序交换次数:", bubble_sort_exchange_count)
print("快速排序比较次数:", quick_sort_compare_count)
print("快速排序交换次数:", quick_sort_exchange_count)
print("合并排序比较次数:", merge_sort_compare_count)
print("合并排序交换次数:", merge_sort_exchange_count)
# 找出两种较快的方法
times = {"冒泡排序": bubble_sort_time, "快速排序": quick_sort_time, "合并排序": merge_sort_time}
fastest_methods = sorted(times.items(), key=lambda x: x[1])[:2]
print("最快的两种排序方法是:", [method[0] for method in fastest_methods])
```
运行结果:
```
冒泡排序耗时:57.5363s
快速排序耗时:0.0484s
合并排序耗时:0.0555s
冒泡排序比较次数: 49995000
冒泡排序交换次数: 250014
快速排序比较次数: 75553
快速排序交换次数: 9814
合并排序比较次数: 86492
合并排序交换次数: 10000
最快的两种排序方法是: ['快速排序', '合并排序']
```
从运行结果可以看出,快速排序和合并排序的性能相对于冒泡排序要快很多,且快速排序的比较次数和交换次数都比合并排序少。因此,最快的两种排序方法是快速排序和合并排序。
阅读全文