【VBA实战】用Excel制作排序算法动画续

server/2024/11/15 4:13:14/

为什么会产生用excel来制作排序算法动画的念头,参见【VBA实战】用Excel制作排序算法动画一文。这篇文章贴出我所制作的所有排序算法动画效果和源码,供大家参考。

冒泡排序:

插入排序:

选择排序:

快速排序:

归并排序:

堆排序:

希尔排序:

完整源码如下。大家也可以直接从这儿下载。

Option Explicit
Public hmap As ObjectSub Sleep(t As Single)  ' T 参数的单位是 秒级Dim time1 As Singletime1 = TimerDoDoEvents '转让控制权,以便让操作系统处理其它的事件Loop While Timer - time1 < t  ' T 参数的单位是 秒级
End Sub'移动单元格
Sub CellMoveTo(rs As Integer, cs As Integer, re As Integer, ce As Integer)Worksheets("Sheet2").Cells(rs, cs).SelectSelection.CutWorksheets("Sheet2").Cells(re, ce).SelectActiveSheet.PasteEnd Sub'同一行两个单元格交换
Sub Swap(row As Integer, col1 As Integer, col2 As Integer)Call CellMoveTo(row, col1, row - 2, col1)Call Sleep(1)Call CellMoveTo(row, col2, row - 1, col2)Call Sleep(1)Dim i%, j%i = col1j = col2Do While i < col2Call CellMoveTo(row - 2, i, row - 2, i + 1)i = i + 1Call CellMoveTo(row - 1, j, row - 1, j - 1)j = j - 1Call Sleep(1)LoopCall CellMoveTo(row - 1, col1, row, col1)Call Sleep(1)Call CellMoveTo(row - 2, col2, row, col2)Call Sleep(1)End Sub'堆的节点交换,只交换数字
Sub HeapSwap(c1 As String, c2 As String)Dim n%Dim clr1 As Long, clr2 As Long, clrf As Longclr1 = 5287936clr2 = 49407Call Color2(c1, clr2)Call Color2(c2, clr2)n = Worksheets("Sheet2").Range(c1).ValueWorksheets("Sheet2").Range(c1).Value = Worksheets("Sheet2").Range(c2).ValueWorksheets("Sheet2").Range(c2).Value = nCall Sleep(1)Call Color2(c1, clr1)Call Color2(c2, clr1)End SubSub Color(row As Integer, col As Integer, clr As Long)Worksheets("Sheet2").Cells(row, col).SelectWith Selection.Interior.Pattern = xlSolid.PatternColorIndex = xlAutomatic.Color = clr.TintAndShade = 0.PatternTintAndShade = 0End With
End SubSub Color1(row As Integer, col As Integer, clr As Long)Call Color(row, col, clr)Call Sleep(1)End SubSub Color2(c As String, clr As Long)Worksheets("Sheet2").Range(c).SelectWith Selection.Interior.Pattern = xlSolid.PatternColorIndex = xlAutomatic.Color = clr.TintAndShade = 0.PatternTintAndShade = 0End WithCall Sleep(1)
End SubSub InitData()Dim clr1 As Longclr1 = 5287936Set hmap = CreateObject("Scripting.Dictionary")hmap.Add 5, "M10"hmap.Add 6, "I14"hmap.Add 7, "Q14"hmap.Add 8, "F17"hmap.Add 9, "L17"hmap.Add 10, "N17"hmap.Add 11, "T17"hmap.Add 12, "D19"hmap.Add 13, "H19"hmap.Add 14, "J19"Dim row%, j%row = 7For j = 5 To 14Dim n%n = Int(100 * Rnd)Worksheets("Sheet2").Cells(row, j) = nCall Color(row, j, clr1)Worksheets("Sheet2").Range(hmap.Item(j)).Value = nWorksheets("Sheet2").Range(hmap.Item(j)).SelectSelection.Interior.Color = clr1Next j
End Sub'堆排序Sub Adjust(r As Integer, last As Integer)Dim f1%, f2%, v1%, v2%, row%Dim clr1 As Long, clr2 As Long, clrf As Longclr1 = 5287936clr2 = 49407clrf = 15773696row = 7f1 = 5 + (r - 5) * 2 + 1f2 = 5 + (r - 5) * 2 + 2v1 = -1v2 = -1If f1 <= last Thenv1 = Worksheets("Sheet2").Cells(row, f1).ValueEnd IfIf f2 <= last Thenv2 = Worksheets("Sheet2").Cells(row, f2).ValueEnd IfIf Worksheets("Sheet2").Cells(row, r) < v1 Or Worksheets("Sheet2").Cells(row, r) < v2 ThenDim s%If v1 > v2 Thens = f1Elses = f2End IfCall Color1(row, r, clr2)Call Color1(row, s, clr2)Call Swap(row, r, s)Call Color1(row, r, clr1)Call Color1(row, s, clr1)Call HeapSwap(hmap.Item(r), hmap.Item(s))Call Adjust(s, last)End IfEnd SubSub HeapSort()Dim i%, j%, row%, last%Dim clr1 As Long, clr2 As Long, clrf As Longrow = 7clr1 = 5287936clr2 = 49407clrf = 15773696last = 14For i = 14 To 6 Step -1Dim t%t = 5 + Int((i - 6) / 2)Call Color1(row, i, clr2)Call Color1(row, t, clr2)If Worksheets("Sheet2").Cells(row, i).Value > Worksheets("Sheet2").Cells(row, t).Value ThenCall Swap(row, t, i)Call HeapSwap(hmap.Item(t), hmap.Item(i))Call Adjust(i, last)End IfCall Color1(row, i, clr1)Call Color1(row, t, clr1)Next iFor i = 14 To 6 Step -1Call Color1(row, 5, clr2)Call Color1(row, i, clr2)Call Swap(row, 5, i)Call Color1(row, 5, clr1)Call Color1(row, i, clrf)Call HeapSwap(hmap.Item(5), hmap.Item(i))Call Color2(hmap.Item(i), clrf)last = last - 1Call Adjust(5, last)Next iCall Color1(row, 5, clrf)Call Color2(hmap.Item(5), clrf)
End Sub'希尔排序
Sub ShellSort()Dim i%, j%, row%, gap%, tmp%Dim clr1 As Long, clr2 As Long, clrf As Longrow = 7clr1 = 5287936clr2 = 49407clrf = 15773696gap = 5Do While gap > 0For i = 5 + gap To 14tmp = Worksheets("Sheet2").Cells(row, i).ValueCall Color1(row, i, clr2)Call CellMoveTo(row, i, row - 2, i)Call Sleep(1)For j = i - gap To 5 Step -gapCall Color1(row, j, clr2)If tmp < Worksheets("Sheet2").Cells(row, j).Value ThenCall CellMoveTo(row, j, row, j + gap)Call Sleep(1)Call Color1(row, j + gap, clr1)Call CellMoveTo(row - 2, j + gap, row - 2, j)Call Sleep(1)ElseCall Color1(row, j, clr1)Exit ForEnd IfNext jCall CellMoveTo(row - 2, j + gap, row, j + gap)Call Sleep(1)Call Color1(row, j + gap, clr1)Next igap = Int(gap / 2)LoopEnd Sub'归并排序
Sub Merge(s1 As Integer, e1 As Integer, s2 As Integer, e2 As Integer)Dim i%, j%, p%, row%Dim clr1 As Long, clr2 As Long, clr3 As Long, clrf As Longrow = 7clr1 = 5287936clr2 = 49407clr3 = 65535clrf = 15773696For i = s1 To e1Call Color(row, i, clr2)Next iFor i = s2 To e2Call Color(row, i, clr3)Next iCall Sleep(1)i = s1j = s2p = s1Do While i <= e1 And j <= e2Do While i <= e1 And Worksheets("Sheet2").Cells(row, i).Value <= Worksheets("Sheet2").Cells(row, j).ValueCall CellMoveTo(row, i, row - 2, p)Call Sleep(1)p = p + 1i = i + 1LoopDo While j <= e2 And Worksheets("Sheet2").Cells(row, j).Value < Worksheets("Sheet2").Cells(row, i).ValueCall CellMoveTo(row, j, row - 2, p)Call Sleep(1)p = p + 1j = j + 1LoopLoopDo While i <= e1Call CellMoveTo(row, i, row - 2, p)Call Sleep(1)p = p + 1i = i + 1LoopDo While j <= e2Call CellMoveTo(row, j, row - 2, p)Call Sleep(1)p = p + 1j = j + 1LoopFor i = s1 To e2Call Color(row - 2, i, clr1)Call CellMoveTo(row - 2, i, row, i)Next iCall Sleep(1)End SubSub MergeSort2(left As Integer, right As Integer)Dim mid%If left >= right ThenExit SubEnd Ifmid = Int((left + right) / 2)Call MergeSort2(left, mid)Call MergeSort2(mid + 1, right)Call Merge(left, mid, mid + 1, right)End SubSub MergeSort()Call MergeSort2(5, 14)
End Sub'快速排序
Sub QuickSort(low As Integer, high As Integer)Dim left%, right%, mend%, row%, i%Dim clr1 As Long, clr2 As Long, clr3 As Long, clrf As Longmend = 14row = 7clr1 = 5287936clr2 = 49407clr3 = 65535clrf = 15773696For i = low To highCall Color(row, i, clr3)Next iCall Sleep(1)If low >= high ThenIf low = high ThenCall Color1(row, low, clrf)End IfExit SubEnd Ifleft = low + 1right = highCall Color1(row, low, clrf)Do While left <= rightCall Color1(row, left, clr2)Do While left <= right And Worksheets("Sheet2").Cells(row, left).Value <= Worksheets("Sheet2").Cells(row, low).ValueCall Color1(row, left, clr1)left = left + 1If left <= right ThenCall Color1(row, left, clr2)End IfLoopCall Color1(row, right, clr2)Do While left <= right And Worksheets("Sheet2").Cells(row, right).Value > Worksheets("Sheet2").Cells(row, low).ValueCall Color1(row, right, clr1)right = right - 1If right >= left ThenCall Color1(row, right, clr2)End IfLoopIf left < right ThenCall Color(row, right, clr2)Call Swap(row, left, right)Call Color(row, left, clr3)Call Color(row, right, clr3)Call Sleep(1)End IfLoopIf low <> left - 1 ThenCall Swap(row, low, left - 1)End IfCall QuickSort(low, left - 2)Call QuickSort(left, high)
End SubSub QuickSort2()Call QuickSort(5, 14)
End Sub'选择排序
Sub SelectionSort()Dim i%, j%, min%, row%Dim clr1 As Long, clr2 As Long, clrf As Long'mend = 14row = 7clr1 = 5287936clr2 = 49407clrf = 15773696For i = 5 To 13min = iCall Color1(row, min, clrf)For j = i + 1 To 14Call Color(row, j, clr2)Call Sleep(1)If Worksheets("Sheet2").Cells(row, j).Value < Worksheets("Sheet2").Cells(row, min).Value ThenCall Color1(row, j, clrf)Call Color1(row, min, clr1)min = jElseCall Color1(row, j, clr1)End IfNext jIf min <> i ThenCall Swap(row, i, min)Call Sleep(1)End IfNext iCall Color(row, 14, clrf)
End Sub'插入排序
Sub InsertSort()Dim i%, j%, row%, tmp%Dim clr1 As Long, clr2 As Long, clrf As Longrow = 7clr1 = 5287936clr2 = 49407clrf = 15773696For i = 6 To 14tmp = Worksheets("Sheet2").Cells(row, i).ValueCall Color1(row, i, clr2)Call CellMoveTo(row, i, row - 1, i)Call Sleep(1)For j = i - 1 To 5 Step -1Call Color1(row, j, clr2)If tmp < Worksheets("Sheet2").Cells(row, j).Value ThenCall CellMoveTo(row, j, row, j + 1)Call Sleep(1)Call Color1(row, j + 1, clr1)Call CellMoveTo(row - 1, j + 1, row - 1, j)Call Sleep(1)ElseCall Color1(row, j, clr1)Exit ForEnd IfNext jCall CellMoveTo(row - 1, j + 1, row, j + 1)Call Sleep(1)Call Color1(row, j + 1, clr1)Next iEnd Sub'冒泡排序
Sub BubbleSort()Dim i%, j%, mend%, row%Dim clr1 As Long, clr2 As Long, clrf As Longmend = 14row = 7clr1 = 5287936clr2 = 49407clrf = 15773696For i = 5 To 13For j = 5 To mend - 1Call Color(row, j, clr2)Call Color(row, j + 1, clr2)Call Sleep(1)If Worksheets("Sheet2").Cells(row, j).Value > Worksheets("Sheet2").Cells(row, j + 1).Value ThenCall Swap(row, j, j + 1)End IfCall Color(row, j, clr1)Call Color(row, j + 1, clr1)Call Sleep(1)Next jCall Color(row, mend, clrf)mend = mend - 1Call Sleep(1)Next iCall Color(row, mend, clrf)End Sub

http://www.ppmy.cn/server/141713.html

相关文章

【网络工程】计算机硬件概述

1. 计算机硬件概述 1.1 定义与组成 计算机硬件是指组成计算机系统的物理设备&#xff0c;包括但不限于中央处理器&#xff08;CPU&#xff09;、存储器、输入设备、输出设备等。这些设备共同构成了计算机的物理基础&#xff0c;使得计算机能够执行各种计算任务。 CPU&#x…

汽车牌照识别系统的设计与仿真(论文+源码)

1设计原理 车牌识别系统的设计是一项利用车辆的动态视频或者静态图像实现牌照区域定位车牌号码识别的技术。其硬件部分通常包括触发设备、拍摄设备、照明设备、图像收集设备、进行车牌号码识别的处理器等&#xff0c;其软件的关键部分包含车牌区域定位的算法、车牌字符的分割算…

Spring框架之适配器模式 (Adapter Pattern)

适配器模式&#xff08;Adapter Pattern&#xff09;详解 适配器模式&#xff08;Adapter Pattern&#xff09;是一种结构型设计模式&#xff0c;它的主要作用是将一个类的接口转换成客户端期望的另一个接口&#xff0c;使原本由于接口不兼容而无法一起工作的类可以协同工作。…

《MYSQL45讲》误删数据怎么办

对误删数据分类的话&#xff0c;有 1.delete 误删行 2.drop table 或者truncate table 语句误删表 3.使用drop database 误删数据库 4.使用rm命令误删整个MYSQL实例 一&#xff0c;误删行 一下操作前置条件是&#xff1a;binlog的格式是row&#xff0c;并且binglog_row_im…

SpringBoot(三)

最佳实践 1.SpringBoot 应用如何编写 引入场景依赖 官方文档 查看自动配置了哪些&#xff08;选做&#xff09; 自己分析&#xff0c;引入场景对应的自动配置一般都生效了配置文件中 debugtrue 开启自动配置报告。 Negative&#xff08;不生效&#xff09;Positive&#xff0…

ctfshow-web入门-反序列化(web260-web264)

目录 1、web260 2、web261 3、web262 4、web263 5、web264 1、web260 要求传入的内容序列化后包含指定内容即可&#xff0c;在 PHP 序列化中&#xff0c;如果键名或值包含 ctfshow_i_love_36D&#xff0c;那么整个序列化结果也会包含这个字符串。 payload&#xff1a; ?…

软件设计师-数据库

上午6分&#xff0c;下午15分&#xff0c;很重要 数据库&#xff0c;长期存储在计算机内的、有组织的、可共享的数据集合&#xff0c;数据库中的数据按照数据模型组织&#xff0c;描述和存储&#xff0c;具有较小的冗余度&#xff0c;较高的数据独立性和易拓展性&#xff0c;并…

【LeetCode】【算法】240. 搜索二维矩阵II

LeetCode 240. 搜索二维矩阵II 题目描述 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 思路 思路&#xff1a;K神真强啊240.搜索二维矩阵II&#xff0…