三種排序方法
今天想給大家分享三種排序方法:選擇排序,直接排序,還有冒泡排序,對於這些排序方法,我的感覺是有些模煳,也對比了一下,不是很懂直接排序法,如果有懂得,給我講一下。這幾個方法主要是排序,有的不重複,有的重複。 方法一:選擇排序法
Option Explicit
Option Base 1
Private Sub cmdsort_Click()
Dim a(10) As Integer, temp As Integer
Dim i As Integer, j As Integer
Randomize
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
Text1 = Text1 & Str(a(i))
Next i
For i = 1 To 10
For j = i + 1 To 10
If a(i) < a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next j
Print a(i)
Next i
End Sub

方法二:直接排序法
Option Explicit
Option Base 1
Private Sub cmdsort_Click()
Dim a(10) As Integer, temp As Integer
Dim i As Integer, j As Integer
Randomize
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
Text1 = Text1 & Str(a(i))
Next i
For i = 1 To 10
For j = i + 1 To 10
If a(i) < a(j) Then
temp = a(i)
a(i) = a(j)
a(j) = temp
End If
Next j
Print a(i)
Next i
end sub

方法三:冒泡排序法
Option Explicit
Option Base 1
Dim a(10) As Integer, temp As Integer, i As Integer, j As Integer, p As String, flag
Private Sub Command1_Click()
'排序
For i = 1 To 9
For j = 1 To 10 - i
If a(j) < a(j + 1) Then
temp = a(j): a(j) = a(j + 1): a(j + 1) = temp
End If
Next j
Next i
For i = 1 To 10
Print a(i)
Next i
End Sub
Private Sub Command2_Click()
Randomize
p = ""
For i = 1 To 10
a(i) = Int(Rnd * 100 + 1)
flag = 0
For j = 1 To i - 1
If a(i) = a(j) Then
flag = 1
i = i - 1
Exit For
End If
Next j
If flag = 0 Then
Print a(i)
End If
Next i
End Sub

最後更新:2017-04-03 21:30:16