现在有多种排序算法,包括冒泡、选择、插入、希尔、归并、快速及堆排序共7种排序算法,请使用python语言对1000以内的一定随机数进行排序,随机数的个数是这样定义的,如你的学号是:20204075117,则生成的随机数个数为20+20+40+75+117=272,即对1000以内的272个随机数进行排序,同时使用柱状图完成排序的动画演示。用python语言
时间: 2024-03-11 12:48:59 浏览: 53
七种排序算法(包括直接插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,简单选择排序,归并排序)
5星 · 资源好评率100%
好的,我会为您展示如何使用Python对1000以内的272个随机数进行排序,并且用柱状图完成排序的动画演示。
首先,我们需要生成272个随机数,可以使用Python的random库来实现。代码如下:
```python
import random
# 生成272个随机数
random_list = [random.randint(1, 1000) for _ in range(272)]
```
接下来,我们可以定义7种排序算法的函数,分别为冒泡、选择、插入、希尔、归并、快速和堆排序。代码如下:
```python
def bubble_sort(nums):
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
def selection_sort(nums):
n = len(nums)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if nums[j] < nums[min_idx]:
min_idx = j
nums[i], nums[min_idx] = nums[min_idx], nums[i]
def insertion_sort(nums):
n = len(nums)
for i in range(1, n):
key = nums[i]
j = i - 1
while j >= 0 and nums[j] > key:
nums[j+1] = nums[j]
j -= 1
nums[j+1] = key
def shell_sort(nums):
n = len(nums)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = nums[i]
j = i
while j >= gap and nums[j-gap] > temp:
nums[j] = nums[j-gap]
j -= gap
nums[j] = temp
gap //= 2
def merge_sort(nums):
if len(nums) > 1:
mid = len(nums) // 2
left_half = nums[:mid]
right_half = nums[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
nums[k] = left_half[i]
i += 1
else:
nums[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
nums[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
nums[k] = right_half[j]
j += 1
k += 1
def quick_sort(nums):
if len(nums) <= 1:
return nums
else:
pivot = nums[0]
left_half = [x for x in nums[1:] if x < pivot]
right_half = [x for x in nums[1:] if x >= pivot]
return quick_sort(left_half) + [pivot] + quick_sort(right_half)
def heap_sort(nums):
def heapify(nums, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and nums[l] > nums[largest]:
largest = l
if r < n and nums[r] > nums[largest]:
largest = r
if largest != i:
nums[i], nums[largest] = nums[largest], nums[i]
heapify(nums, n, largest)
n = len(nums)
for i in range(n//2 - 1, -1, -1):
heapify(nums, n, i)
for i in range(n-1, 0, -1):
nums[i], nums[0] = nums[0], nums[i]
heapify(nums, i, 0)
```
接下来,我们可以使用matplotlib库来完成柱状图的动画演示。代码如下:
```python
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 动画演示
fig, ax = plt.subplots()
def update(num):
ax.clear()
ax.bar(range(len(random_list)), random_list, color='blue')
globals()[sort_methods[num]](random_list)
ax.bar(range(len(random_list)), random_list, color='red')
ax.set_title(sort_methods[num])
return ax
sort_methods = ['bubble_sort', 'selection_sort', 'insertion_sort', 'shell_sort', 'merge_sort', 'quick_sort', 'heap_sort']
ani = FuncAnimation(fig, update, frames=range(len(sort_methods)), repeat=True)
plt.show()
```
最后,我们将所有代码整合在一起,完整代码如下:
```python
import random
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 生成272个随机数
random_list = [random.randint(1, 1000) for _ in range(272)]
def bubble_sort(nums):
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
def selection_sort(nums):
n = len(nums)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if nums[j] < nums[min_idx]:
min_idx = j
nums[i], nums[min_idx] = nums[min_idx], nums[i]
def insertion_sort(nums):
n = len(nums)
for i in range(1, n):
key = nums[i]
j = i - 1
while j >= 0 and nums[j] > key:
nums[j+1] = nums[j]
j -= 1
nums[j+1] = key
def shell_sort(nums):
n = len(nums)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = nums[i]
j = i
while j >= gap and nums[j-gap] > temp:
nums[j] = nums[j-gap]
j -= gap
nums[j] = temp
gap //= 2
def merge_sort(nums):
if len(nums) > 1:
mid = len(nums) // 2
left_half = nums[:mid]
right_half = nums[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
nums[k] = left_half[i]
i += 1
else:
nums[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
nums[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
nums[k] = right_half[j]
j += 1
k += 1
def quick_sort(nums):
if len(nums) <= 1:
return nums
else:
pivot = nums[0]
left_half = [x for x in nums[1:] if x < pivot]
right_half = [x for x in nums[1:] if x >= pivot]
return quick_sort(left_half) + [pivot] + quick_sort(right_half)
def heap_sort(nums):
def heapify(nums, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and nums[l] > nums[largest]:
largest = l
if r < n and nums[r] > nums[largest]:
largest = r
if largest != i:
nums[i], nums[largest] = nums[largest], nums[i]
heapify(nums, n, largest)
n = len(nums)
for i in range(n//2 - 1, -1, -1):
heapify(nums, n, i)
for i in range(n-1, 0, -1):
nums[i], nums[0] = nums[0], nums[i]
heapify(nums, i, 0)
# 动画演示
fig, ax = plt.subplots()
def update(num):
ax.clear()
ax.bar(range(len(random_list)), random_list, color='blue')
globals()[sort_methods[num]](random_list)
ax.bar(range(len(random_list)), random_list, color='red')
ax.set_title(sort_methods[num])
return ax
sort_methods = ['bubble_sort', 'selection_sort', 'insertion_sort', 'shell_sort', 'merge_sort', 'quick_sort', 'heap_sort']
ani = FuncAnimation(fig, update, frames=range(len(sort_methods)), repeat=True)
plt.show()
```
运行代码后,您将看到柱状图的动画演示,同时也可以看到排序算法的实时排序结果。
阅读全文