9.3 数组应用例题(根据时间可以选择一个讲!) 根据需要,选择1-2道下面的题目,给学生讲清楚算法的思想!
1 排序
例5-3 选择排序法 直接排序法
Option Explicit
Option Base 1
Private Sub CmdSort_Click()
Dim Sort(10) As Integer, Temp As Integer Dim I As Integer, J As Integer Randomize For I = 1 To 10
Sort(I) = Int(Rnd * (100 - 1)) + 1 Text1 = Text1 & Str(Sort(I)) Next I
For I = 1 To 9
For J = I + 1 To 10
If Sort(I) > Sort(J) Then Temp = Sort(I) Sort(I) = Sort(J) Sort(J) = Temp
End If Next J
Text2 = Text2 & Str(Sort(I)) Next I
Text2 = Text2 & Str(Sort(10)) End Sub
Option Explicit Option Base 1
Private Sub CmdSort_Click()
Dim Sort(10) As Integer, Temp As Integer Dim I As Integer, J As Integer Dim Pointer As Integer Randomize For I = 1 To 10
Sort(I) = Int(Rnd * (100 - 1)) + 1 Text1 = Text1 & Str(Sort(I)) Next I
For I = 1 To 9 Pointer = I
For J = I + 1 To 10
If Sort(Pointer) > Sort(J) Then Pointer = J End If Next J
If I <> Pointer Then Temp = Sort(I) Sort(I) = Sort(Pointer) Sort(Pointer) = Temp End If
Text2 = Text2 & Str(Sort(I)) Next I
Text2 = Text2 & Str(Sort(10))
End Sub
2 查找
例:5-4 顺序查找程序 (界面见P105) 先介绍For Each –next 结构的语句: Option Explicit
Option Base 1 Dim Search As Variant