Insertion sort VB .Net Core code example

Example of insertion sort code in VB .Net Core

Module Program
    Sub Main(args As String())
        Dim numbers As Integer()
        numbers = {7, 3, 5, 8, 9, 2, 7}
        Dim sorter As InsertionSort = New InsertionSort()
        sorter.Sort(numbers)
        Dim value As String = String.Join(",", numbers)
        Console.WriteLine("[{0}]", value)

    End Sub
End Module
Public Class InsertionSort
    Public Sub Sort(ByRef array As Integer())
        For i = 0 To array.Length - 1
            Dim current As Integer = array(i)
            Dim j As Integer = i - 1
            While j >= 0
                If array(j) > current Then
                    array(j + 1) = array(j)
                    j = j - 1
                Else
                    Exit While
                End If
            End While
            array(j + 1) = current

        Next
    End Sub
End Class

Subscribe to Code, Query, Ship, and Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe