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

Here, we are going to learn how to sort an integer array using Merge sort in VB.Net?
Submitted by Nidhi, on December 10, 2020 [Last updated : March 06, 2023]

Sort an integer array using Merge sort

Here, we will create an array of integers and then read elements from the user. After we sort the array element using merge sort and then print sorted array on the console screen.

Program/Source Code:

The source code to sort an integer array using Merge sort is given below. The given program is compiled and executed successfully.

VB.Net code 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

Output

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 . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(). In the Main() we created an array arr of five elements and we created three functions Sort() and Merging() to sort array elements using the Merge Sort technique.

VB.Net Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.