VB.Net Tutorial/Collections/Sort

Материал из VB Эксперт
Перейти к: навигация, поиск

Creates random numbers and sorts them using bubble sort

<source lang="vbnet">Module Tester

  Sub Main()
     Dim array As Integer() = New Integer(9) {}
     Dim randomNumber As Random = New Random()
     Dim i As Integer
     For i = 0 To array.GetUpperBound(0)
        array(i) = randomNumber.Next(100)
        Console.Write(array(i)& " ")
     Next
     BubbleSort(array)
     Console.WriteLine("")
     For i = 0 To array.GetUpperBound(0)
        Console.Write(array(i)& " ")
     Next
  End Sub 
  Sub BubbleSort(ByVal sortArray As Integer())
     Dim pass, i As Integer
     For pass = 1 To sortArray.GetUpperBound(0)
        For i = 0 To sortArray.GetUpperBound(0) - 1
           If sortArray(i) > sortArray(i + 1) Then
              Swap(sortArray, i)
           End If
        Next
     Next
  End Sub " BubbleSort
  Sub Swap(ByVal swapArray As Integer(), _
     ByVal first As Integer)
     Dim hold As Integer
     hold = swapArray(first)
     swapArray(first) = swapArray(first + 1)
     swapArray(first + 1) = hold
  End Sub " Swap

End Module</source>

11 18 28 60 97 59 23 23 25 14
11 14 18 23 23 25 28 59 60 97 "