Selection sort in vb.net

Vb program for Selection sort. Here mentioned other language solution.

' Include namespace system
Imports System 
'  Vb.net program for selection sort
public Class MySort
    '  Swap the list element
    Public Sub swap(ByVal arr As Integer(), 
      ByVal x As Integer, ByVal y As Integer)
        '  x and y are index of list
        Dim temp As Integer = arr(x)
        arr(x) = arr(y)
        arr(y) = temp
    End Sub
    Public Sub selectionSort(ByVal arr As Integer(), 
      ByVal n As Integer)
        Dim min As Integer = 0
        '  Execute loop from 0..n
        Dim i As Integer = 0
        While i < n
            '  Get current index
            min = i
            Dim j As Integer = i + 1
            While j < n
                if (arr(min) > arr(j)) Then
                    '  Get the minimum element index
                    min = j
                End If
            j += 1
            End While
            if (i <> min) Then
                '  Swap minimum element at i index
                Me.swap(arr, i, min)
            End If
        i += 1
        End While
    End Sub
    '  Display list elements
    Public Sub display(ByVal arr As Integer(), 
      ByVal n As Integer)
        Dim i As Integer = 0
        While i < n
            '  Display element value
            Console.Write(" {0}", arr(i))
        i += 1
        End While
        Console.Write( vbLf )
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim task As MySort = New MySort()
        '  Array of integer elements
        Dim arr As Integer() =
        {8, 2, 3, 8, 1, 3, 73, 121, 54, 23, 84, 13, 67, 23, 52}
        '  Get the size of list
        Dim n As Integer = arr.Length
        Console.WriteLine(" Before Sort :")
        task.display(arr, n)
        '  Test
        task.selectionSort(arr, n)
        Console.WriteLine(" After Sort :")
        task.display(arr, n)
    End Sub
End Class

Output

 Before Sort :
 8 2 3 8 1 3 73 121 54 23 84 13 67 23 52
 After Sort :
 1 2 3 3 8 8 13 23 23 52 54 67 73 84 121


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved