ADVERTISEMENT
ADVERTISEMENT

VB.Net program to sort an array in ascending order using selection sort

Here, we are going to learn how to sort an array in ascending order using selection sort in VB.Net?
Submitted by Nidhi, on December 08, 2020

Here, we will sort an array of integers in the ascending order using selection sort, and then print sorted array on the console screen.

Program/Source Code:

The source code to sort an array in ascending order using selection sort is given below. The given program is compiled and executed successfully.

'VB.Net program to sort an integer array in the 
'ascending order using selection sort.

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim min As Integer=0
        Dim temp As Integer = 0

        Dim loop1 As Integer = 0
        Dim loop2 As Integer = 0
        Dim i As Integer = 0

        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 array in ascending order using selection sort.
        For loop1 = 0 To 4 Step 1
            min = loop1
            For loop2 = loop1 + 1 To 4
                If arr(loop2) < arr(min) Then
                    min = loop2
                End If
            Next
            temp = arr(loop1)
            arr(loop1) = arr(min)
            arr(min) = temp
        Next

        Console.WriteLine("Array 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]: 43
Element[1]: 32
Element[2]: 15
Element[3]: 56
Element[4]: 76
Array after sorting:
15 32 43 56 76
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 four variables min, temp , loop1, and loop2 that are initialized with 0.

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

In the above code, we read the elements of the array from the user.

'Sort array in ascending order using selection sort.
For loop1 = 0 To 4 Step 1
    min = loop1
    For loop2 = loop1 + 1 To 4
        If arr(loop2) < arr(min) Then
            min = loop2
        End If
    Next
    temp = arr(loop1)
    arr(loop1) = arr(min)
    arr(min) = temp
Next

Console.WriteLine("Array after sorting: ")
For i = 0 To 4 Step 1
    Console.Write("{0} ", arr(i))
Next
Console.WriteLine()

Using above given code, we sort the array in the ascending order using selection sort and print the sorted array on the console screen.

VB.Net Array Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.