January 24, 2014

Permutation of all elements of the array in ascending order.

The program builds all the elements of the array in ascending order.
Programming language: Visual Basic.

Code:
Module Module1

    Sub Main()
        Const x As Integer = 20
        Dim f(x), temp As Integer
        Randomize()
        Console.Write("Random = ")

        For i = 1 To x
            f(i) = Rnd() * 100
            Console.Write(f(i) & "; ")
        Next
        Console.WriteLine()
        Console.Write("Result = ")
        For i = 1 To x - 1
            For g = 1 To x - i
                If f(g) > f(g + 1) Then
                    temp = f(g)
                    f(g) = f(g + 1)
                    f(g + 1) = temp
                End If

            Next
        Next
        Console.WriteLine()
        For i = 1 To x
            Console.Write(f(i) & "; ")
        Next
        Console.ReadKey()
    End Sub

End Module

No comments:

Post a Comment