下面程式從比大小、交換、三個數排序、四個數排序、N個數排序,循序詳細解說氣泡排序的構成,讀者可利用「F8」逐步執行,了解每個指令的執行情形。
1 Public Class Form1
2
3 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
4
5 Dim a, b, c, t As Integer
6 a = 20
7 b = 10
8
9 '比大小
10 If a > b Then Debug.Print(a & ">" & b)
11 If a < b Then Debug.Print(a & "<" & b)
12 If a = b Then Debug.Print(a & "=" & b)
13
14 '交換
15 Debug.Print(a & "," & b)
16 t = a
17 a = b
18 b = t
19 Debug.Print(a & "," & b)
20
21 '由小至大
22 a = 10
23 b = 20
24 Debug.Print(a & "," & b)
25 If a > b Then
26 t = a
27 a = b
28 b = t
29 End If
30 Debug.Print(a & "," & b)
31
32 '由小至大--三個數
33 a = 20
34 b = 30
35 c = 15
36 Debug.Print(a & "," & b & "," & c)
37 For i = 1 To 2
38 If a > b Then
39 t = a
40 a = b
41 b = t
42 End If
43 If b > c Then
44 t = b
45 b = c
46 c = t
47 End If
48 Next i
49 Debug.Print(a & "," & b & "," & c)
50
51 '由小至大--四個數
52 Dim z(5) As Integer
53 z(1) = 20
54 z(2) = 30
55 z(3) = 15
56 z(4) = 5
57 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4))
58 For i = 1 To 3
59 If z(1) > z(2) Then
60 t = z(1)
61 z(1) = z(2)
62 z(2) = t
63 End If
64 If z(2) > z(3) Then
65 t = z(2)
66 z(2) = z(3)
67 z(3) = t
68 End If
69 If z(3) > z(4) Then
70 t = z(3)
71 z(3) = z(4)
72 z(4) = t
73 End If
74 Next i
75 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4))
76
77 '由小至大--四個數--重覆動作改寫為迴路
78 ' Dim z(5) As Integer
79 z(1) = 20
80 z(2) = 30
81 z(3) = 15
82 z(4) = 5
83 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4))
84 For i = 1 To 3
85 For j As Integer = 1 To 3
86 If z(j) > z(j + 1) Then
87 t = z(j)
88 z(j) = z(j + 1)
89 z(j + 1) = t
90 End If
91 Next j
92 Next i
93 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4))
94
95 '由小至大--四個數--重覆動作改寫為迴路--改寫為N筆適用--輸出至MsgBox
96 ' Dim z(5) As Integer
97 Dim n As Integer = 5
98 z(1) = 20
99 z(2) = 30
100 z(3) = 15
101 z(4) = 5
102 z(5) = 10
103 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4) & "," & z(5))
104 For i = 1 To n - 1
105 For j As Integer = 1 To n - 1
106 If z(j) > z(j + 1) Then
107 t = z(j)
108 z(j) = z(j + 1)
109 z(j + 1) = t
110 End If
111 Next j
112 Next i
113 Debug.Print(z(1) & "," & z(2) & "," & z(3) & "," & z(4) & "," & z(5))
114 Dim msgStr As String = ""
115 For i = 1 To n
116 msgStr = msgStr & z(i) & " "
117 Next
118 MsgBox(msgStr)
119 End Sub
120 End Class
標籤雲
visual basic 2008
(157)
訊息分享
(111)
Visual Studio 2008
(64)
wpf
(37)
每日一句
(35)
cpp
(30)
python
(26)
Silverlight
(22)
C++
(18)
Network Security
(15)
全國技藝競竇
(14)
好文分享
(11)
.Net
(10)
Blogger
(10)
名詞解釋
(10)
研討會
(10)
Excel
(9)
書籍介紹
(9)
每日一小品
(9)
電腦黑白講
(8)
Visual Studio 201X
(7)
分享
(7)
網頁設計
(7)
CSS
(5)
Algorithm
(4)
Network
(3)
PHP
(3)
Access
(2)
SA
(2)
VB.Net
(2)
VBA
(2)
WireShark
(2)
Word
(2)
php html
(2)
其他好東東
(2)
分類整理
(2)
Asp.Net
(1)
Batch
(1)
Blockly
(1)
IT News
(1)
OpenAI
(1)
SE
(1)
W7
(1)
Writer
(1)
app inv2
(1)
dfs
(1)
vex vr sample
(1)
影像處理練習
(1)
黑白講
(1)
2008年4月18日 星期五
氣泡排序詳解
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言