2008年12月23日 星期二

選擇排序














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

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

'選擇排序 - 由小至大
For j = 0 To 3
For i = j + 1 To 4
If a(j) > a(i) Then
Dim t = a(j)
a(j) = a(i)
a(i) = t
End If
Next i
Next j

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




沒有留言:

張貼留言