VB各种排序方法.ppt
- 格式:ppt
- 大小:294.00 KB
- 文档页数:14
vb各种排序算法'此文飞本人原创具体出处未知Option ExplicitDim Sums(9999) As Long, Sumb(9999) As Long '生成数据数量可自己设置Private blnSort As Boolean '排序方向Private Declare Function SendMessageFind Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wmsg As Long, ByVal wparam As Integer, ByVal lparam As String) As Long Private Declare Function timeGetTime Lib "winmm.dll" () As LongDim T As LongPrivate Sub Command1_Click()Dim i As Long, z As Long, j As LongList1.ClearDoEventsList1.Visible = FalseFor i = 0 To UBound(Sums)nn:Randomizez = 99999 * Rnd + 9j = SendMessageFind(List1.hWnd, &H18F, 0, z)If j > -1 ThenGoTo nnElseSums(i) = zSumb(i) = zList1.AddItem Sums(i)End IfNextList1.Visible = TrueMe.Caption = "共产生数据:" & UBound(Sums) + 1 & " 项"End SubPrivate Sub Command2_Click()Dim ti As Integer, i As LongList2.ClearDoEventsFor i = 0 To UBound(Sumb)Sums(i) = Sumb(i)NextblnSort = Option1(0).ValueT = timeGetTimeIf Option2(0).Value = True ThenCall mpsort(Sums) '冒泡排序ti = 0End IfIf Option2(1).Value = True ThenCall insort(Sums) '插入排序ti = 1End IfIf Option2(2).Value = True ThenCall QuickSort(LBound(Sums), UBound(Sums)) '快速排序ti = 2End IfIf Option2(3).Value = True ThenCall selctsort(Sums) '选择排序ti = 3End IfIf Option2(4).Value = True ThenCall hirsort(Sums) '希尔排序ti = 4End IfIf Option2(5).Value = True ThenCall duisort(Sums) '堆排序ti = 5End IfIf Option2(6).Value = True ThenCall nsort(Sums) '打乱次序ti = 6End IfLabel1(ti).Caption = timeGetTime - TList2.Visible = FalseDoEventsFor i = 0 To UBound(Sums)List2.AddItem Sums(i)NextList2.Visible = TrueMe.Caption = "成功对:" & UBound(Sums) + 1 & " 项数据进行了排序,用时: " & Label1(ti).Caption & " 毫秒"Exit SubEnd SubPrivate Sub Command3_Click()List1.ClearList2.ClearMe.Caption = "六种排序"End SubPrivate Sub nsort(ByRef arrtosort() As Long)Dim i As Long, j As Long, tmp As LongFor i = LBound(arrtosort) To UBound(arrtosort)j = (UBound(arrtosort) - i) * Rnd + iIf i <> j Thentmp = arrtosort(i)arrtosort(i) = arrtosort(j)arrtosort(j) = tmpEnd IfNext iEnd SubPrivate Sub mpsort(ByRef arrtosort() As Long) '冒泡排序'经过n-1趟子排序完成的,它的时间复杂度为O(n^2)'优点:1.“编程复杂度”很低,很容易写出代码;2.具有稳定性Dim i As Long, j As Long, M As Long, tmp As LongM = UBound(arrtosort) 'm 等于数组上标Do While M '至m等于数组下标j = M - 1M = 0If blnSort ThenFor i = 0 To jIf arrtosort(i) > arrtosort(i + 1) Then '找到后者大于前者地数tmp = arrtosort(i) '两者互换arrtosort(i) = arrtosort(i + 1)arrtosort(i + 1) = tmpM = i '从该位置开始继续查找End IfNext iElseFor i = 0 To jIf arrtosort(i) < arrtosort(i + 1) Thentmp = arrtosort(i)arrtosort(i) = arrtosort(i + 1)arrtosort(i + 1) = tmpM = iEnd IfNext iEnd IfLoopEnd SubPrivate Sub insort(ByRef arrtosort() As Long) '插入排序'插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据'算法适用于少量数据的排序,时间复杂度为O(n^2)。
第三章顺序结构程序设计§1、预备知识一、结构化程序设计的三种基本结构:顺序结构、分支结构、循环结构顺序结构:就是从头到尾依次执行每一个语句分支结构:根据不同的条件执行不同的语句或者语句体循环结构:重复的执行语句或者语句体,达到重复执行一类操作的目的二、语句概念:语句是执行具体操作的指令。
VB程序中一行代码称为一条语句。
例如:LetA=3程序中的每条语句都必须遵从语句的语法规则和格式。
1、语句格式的符号规定<>尖括号;必选项[]方括号;可选项|竖线;用来分隔多个选择项,选其中之一项…省略号;表示同类项目的重复出现例如:[<对象名> .]Print [<表达式表>]2、语句书写规则(1)每行通常写一条语句;若将几个语句写在一行,语句间需要用冒号分隔;例如:A=3:B=4(2)一个语句行不能超过1023个字符,如果太长,可用“_”续行;例如:Form1.print “I am a _Student.”(3)与其他高级语言一样,在VB中使用的分号、引号、括号等符号都是英文状态下的半角符号,而不能使用中文状态下的全角符号。
例如:If a<5 Then x=10Print "x="; x不能写成If a〈5 Then x=10Print “x=”;x§2、顺序结构的基本语句和方法一、赋值语句Let格式:[Let]<变量名>=<表达式>[〈对象名〉.]<属性名>=<表达式>功能:首先计算赋值号右边表达式的值,然后,将值赋予左边变量或对象属性。
例1+4=? 程序Private Sub Form_Activate()Dim a&, b&, m&a = 1:b = 4m = a + bPrint mEnd Sub例3_2_1Private Sub Form_Click()Dim a As Long, b As String, c As Booleana = 100.23b = "100.23"a = bc = 1 > 5 And -1End Sub说明:(1)当数值赋值于字符变量时,自动转换为字符,但是字符数据赋值与数值变量时,必须为数字,否则出错。