VB.net Program to Sort an Integer Array Using Merge Sort

You are currently viewing VB.net Program to Sort an Integer Array Using Merge Sort

VB.Net program to sort an integer array using Merge sort

'VB.Net program to sort an integer array using Merge sort.

Module Module1
    Sub merging(ByVal a() As Integer, ByVal low As Integer, ByVal mid As Integer, ByVal high As Integer)

        Dim l1 As Integer = 0
        Dim l2 As Integer = 0
        Dim i As Integer = 0
        Dim b As Integer() = New Integer(4) {}

        l1 = low
        l2 = mid + 1
        i = low

        While (l1 <= mid AndAlso l2 <= high)
            If (a(l1) <= a(l2)) Then
                b(i) = a(l1)
                l1 = l1 + 1
            Else
                b(i) = a(l2)
                l2 = l2 + 1
            End If
            i = i + 1
        End While

        While (l1 <= mid)
            b(i) = a(l1)
            i = i + 1
            l1 = l1 + 1
        End While

        While (l2 <= high)
            b(i) = a(l2)
            i = i + 1
            l2 = l2 + 1
        End While

        For i = low To high Step 1
            a(i) = b(i)
        Next

    End Sub

    Sub sort(ByVal a() As Integer, ByVal low As Integer, ByVal high As Integer)
        Dim mid As Integer = 0

        If (low < high) Then
            mid = (low + high) \ 2
            sort(a, low, mid)
            sort(a, mid + 1, high)
            merging(a, low, mid, high)
        Else
            GoTo out
        End If
out:
        Console.Write("")
    End Sub
    Sub Main()
        Dim arr As Integer() = New Integer(5) {}

        Console.WriteLine("Enter array elements: ")
        For i = 0 To 4 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        sort(arr, 0, 4)

        Console.WriteLine("Array element after sorting: ")
        For i = 0 To 4 Step 1
            Console.Write("{0} ", arr(i))
        Next
        Console.WriteLine()
    End Sub
End Module
Enter array elements:
Element[0]: 23
Element[1]: 45
Element[2]: 64
Element[3]: 67
Element[4]: 24
Array element after sorting:
23 24 45 64 67
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply