VB.net Program to Sort an Array in Descending Order Using Selection Sort

You are currently viewing VB.net Program to Sort an Array in Descending Order Using Selection Sort

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

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

Module Module1

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Dim max 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 descending order using selection sort.
        For loop1 = 0 To 4 Step 1
            max = loop1
            For loop2 = loop1 + 1 To 4
                If arr(loop2) > arr(max) Then
                    max = loop2
                End If
            Next
            temp = arr(loop1)
            arr(loop1) = arr(max)
            arr(max) = 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
Enter array elements:
Element[0]: 23
Element[1]: 65
Element[2]: 87
Element[3]: 34
Element[4]: 12
Array after sorting:
87 65 34 23 12
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