三种排序方法

   今天想给大家分享三种排序方法:选择排序,直接排序,还有冒泡排序,对于这些排序方法,我的感觉是有些模糊,也对比了一下,不是很懂直接排序法,如果有懂得,给我讲一下。这几个方法主要是排序,有的不重复,有的重复。
         方法一:选择排序法
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
三种排序方法_第1张图片
           方法二:直接排序法
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               
三种排序方法_第2张图片

          方法三:冒泡排序法
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
三种排序方法_第3张图片

你可能感兴趣的:(三种排序方法)