How to print a list when first element is first max and second element is first min and so on in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Sub print_first_max_second_min(ByVal arr As Integer())
        Array.Sort(arr)
		
        Dim size As Integer = arr.Length
        Dim i As Integer = 0, j As Integer = size - 1

        While i < j
            Console.Write(arr(Math.Max(System.Threading.Interlocked.Decrement(j), j + 1)) & " ")
            Console.Write(arr(Math.Min(System.Threading.Interlocked.Increment(i), i - 1)) & " ")
        End While

        If size Mod 2 <> 0 Then Console.WriteLine(arr(i))
    End Sub

    Public Shared Sub Main()
        Dim arr As Integer() = {13, 5, 2, 10, 4, 9, 7, 8, 559}
			
        print_first_max_second_min(arr)
    End Sub
End Class
	
	
	
' run:
'
' 559 2 13 4 10 5 9 7 8
'

 



answered Dec 8, 2021 by avibootz
...