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,907 questions

51,839 answers

573 users

How to rearrange an array such that every odd index element is greater than its previous in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Sub swap(ByVal arr As Integer(), ByVal i As Integer, ByVal j As Integer)
        Dim temp As Integer = arr(i)
        arr(i) = arr(j)
        arr(j) = temp
    End Sub

    Public Shared Sub rearrangeArray(ByVal arr As Integer())
        Dim size As Integer = arr.Length

        For i As Integer = 0 To size - 1 - 1 Step 2
            If arr(i) > arr(i + 1) Then
                swap(arr, i, i + 1)
            End If
        Next

        If (size And 1) <> 0 Then

            For i As Integer = size - 1 To 0 + 1 Step 2
                If arr(i) > arr(i - 1) Then
                    swap(arr, i, i - 1)
                End If
            Next
        End If
    End Sub

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

        rearrangeArray(arr)

        Console.WriteLine(String.Join(" ", arr))
    End Sub
End Class




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

 



answered Oct 1, 2023 by avibootz
...