Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to rearrange a given sorted array in maximum minimum form with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub RearrangeArrayMaxMinForm(ByVal array As Integer())
        Dim size As Integer = array.Length
		
        Dim lst As List(Of Integer) = New List(Of Integer)()
		
        Dim left As Integer = 0
        Dim right As Integer = size - 1
        Dim odd_index As Boolean = True

        For i As Integer = 0 To size - 1
            If odd_index Then
                lst.Add(array(Math.Max(System.Threading.Interlocked.Decrement(right), right + 1)))
            Else
                lst.Add(array(Math.Min(System.Threading.Interlocked.Increment(left), left - 1)))
            End If

            odd_index = Not odd_index
        Next

        For i As Integer = 0 To size - 1
            array(i) = lst(i)
        Next
    End Sub

    Private Shared Sub PrintArray(ByVal array As Integer())
        For i As Integer = 0 To array.Length - 1
            Console.Write(array(i) & " ")
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim array As Integer() = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9}

        RearrangeArrayMaxMinForm(array)

        PrintArray(array)
    End Sub
End Class




' run:
'
' 9 1 8 2 7 3 6 4 5
'

 



answered Sep 9, 2022 by avibootz
...