冒泡排序可视化
时间: 2023-06-28 12:11:55 浏览: 59
好的,以下是Python实现的冒泡排序可视化代码:
```
import random
import matplotlib.pyplot as plt
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]
# 可视化
plt.clf()
plt.bar(range(len(arr)), arr)
plt.pause(0.1)
return arr
# 生成随机数组
arr = [random.randint(1, 100) for _ in range(20)]
# 可视化初始状态
plt.bar(range(len(arr)), arr)
plt.ion()
plt.show()
# 排序并可视化
sorted_arr = bubble_sort(arr)
plt.clf()
plt.bar(range(len(sorted_arr)), sorted_arr)
plt.show()
```
运行代码可以看到,每交换一次两个元素,程序就会更新一次可视化。这样可以直观地观察到排序的过程。
相关问题
opencv冒泡排序可视化
冒泡排序是一种简单的排序算法,可以通过OpenCV进行可视化。下面是一个使用OpenCV实现冒泡排序可视化的示例代码:
```python
import cv2
import numpy as np
def bubble_sort_visualization(arr):
n = len(arr)
img = np.zeros((500, 500, 3), dtype=np.uint8)
img.fill(255)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
# Swap the elements
arr[j], arr[j+1] = arr[j+1], arr[j]
# Visualize the current state of the array
img = np.zeros((500, 500, 3), dtype=np.uint8)
img.fill(255)
for k in range(n):
x = k * 50 + 50
y = 450 - arr[k] * 20
cv2.rectangle(img, (x, y), (x+40, 450), (0, 0, 0), -1)
cv2.imshow('Bubble Sort Visualization', img)
cv2.waitKey(100)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 测试代码
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort_visualization(arr)
```
运行上述代码,将会打开一个窗口,显示排序过程的可视化效果。每次交换元素时,窗口中的矩形条会根据当前数组的状态进行重新绘制,直到完成排序。
请注意,这只是一个简单的冒泡排序可视化示例,仅用于说明如何使用OpenCV进行可视化。实际应用中,可能需要更加复杂的算法和数据结构来处理更大规模的数据。
java冒泡排序可视化
以下是一个简单的 Java 冒泡排序可视化的示例代码:
```java
import java.util.Arrays;
public class BubbleSortVisualization {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 4, 7, 6, 1, 3, 8};
System.out.println("Before sorting: " + Arrays.toString(arr));
bubbleSort(arr);
System.out.println("After sorting: " + Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
int temp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换 arr[j] 和 arr[j+1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// 打印当前的数组状态
System.out.println(Arrays.toString(arr));
}
}
}
}
}
```
在上述代码中,我们使用了 `Arrays.toString()` 方法来打印数组。在冒泡排序的循环中,每当有元素交换时,就会打印当前的数组状态,从而可视化排序的过程。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)