2008年12月23日 星期二

氣泡排序


Public Class Form1
Dim i, j
Dim str1 = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原始資料
Dim a() = {3, 2, 5, 1, 4}

'排序前資料輸出
For i = 0 To 4
str1 = str1 & a(i) & " "
Next i
MsgBox(str1)

'排序
For j = 0 To 3
For i = 0 To 3
If a(i) > a(i + 1) Then
Dim t = a(i)
a(i) = a(i + 1)
a(i + 1) = t
End If
Next i
Next j

'排序後資料輸出
str1 = ""
For i = 0 To 4
str1 = str1 & a(i) & " "
Next i
MsgBox(str1)
End Sub
End Class

沒有留言:

張貼留言