冒泡排序 - vb.net

时间:2023-12-27 15:58:43

Imports System.Threading

Module Module1

    Sub Main()

        Dim int_array(9) As Integer
        Dim rnd As New Random

        '填充数组元素
        For i As Integer = 0 To int_array.Length - 1
            int_array(i) = rnd.Next(0, 100)
            Console.Write(int_array(i) & ", ")
            Thread.Sleep(150)
        Next

        Console.WriteLine(vbCrLf & "-------------------------------")

        Dim x As Integer
        x = 2
        Do
            For i As Integer = 0 To int_array.Length - x
                If (int_array(i) > int_array(i + 1)) Then
                    Exchange(int_array(i), int_array(i + 1))
                End If
            Next i

            x += 1

            '向控制台输出结果
            For j As Integer = 0 To int_array.Length - 1
                Console.Write(int_array(j) & ", ")
            Next j
            Console.WriteLine()
        Loop While (x <= int_array.Length)

        Console.ReadKey()
    End Sub

    '交换值
    Public Sub Exchange(ByRef val1 As Integer, ByRef val2 As Integer)
        Dim temp As Integer
        temp = val1
        val1 = val2
        val2 = temp
    End Sub

End Module