How to sort an array in descending order using selection sort with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Sub selection_sort_descending(ByVal arr As Integer())
        Dim len As Integer = arr.Length

        For i As Integer = 0 To len - 1 - 1
            Dim max_i As Integer = i
            For j As Integer = i + 1 To len - 1
                If arr(j) > arr(max_i) Then
                    max_i = j
                End If
            Next

            Dim max As Integer = arr(max_i)
            arr(max_i) = arr(i)
            arr(i) = max
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim arr As Integer() = New Integer() {2, 141, 3, 4, 21, 13, 30, 50}
        
		selection_sort_descending(arr)

        For Each i As Integer In arr
            Console.Write(i & " ")
        Next
    End Sub
End Class




' run:
'
'  141 50 30 21 13 4 3 2
'

 



answered Feb 19, 2024 by avibootz
...