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

51,793 answers

573 users

How to merge elements of two sorted not equal arrays by maintaining the sorted order in VB.NET

2 Answers

0 votes
Imports System

Public Class Program
    Public Shared Sub merge_sorted_not_equal_arrays(array1 As Integer(), array2 As Integer())
        Dim size1 As Integer = array1.Length
        Dim size2 As Integer = array2.Length

		For i As Integer = size2 - 1 To 0 STEP -1
            Dim j As Integer, last1 As Integer = array1(size1 - 1)
            j = size1 - 2

			While j >= 0 AndAlso array1(j) > array2(i)
                array1(j + 1) = array1(j)
                j -= 1
            End While

            If last1 > array2(i) Then
                array1(j + 1) = array2(i)
                array2(i) = last1
            End If
        Next
    End Sub

    Public Shared Sub Main(ByVal args As String())
        Dim array1 As Integer() = New Integer() {1, 4, 6, 8, 10}
        Dim array2 As Integer() = New Integer() {2, 3, 5, 9}
	
        merge_sorted_not_equal_arrays(array1, array2)
	
        Console.WriteLine(String.Join(" ", array1))
        Console.WriteLine(String.Join(" ", array2))
    End Sub
End Class
 
         
         
         
' run:
'
' 1 2 3 4 5
' 6 8 9 10
'

 



answered Sep 16, 2023 by avibootz
edited Sep 16, 2023 by avibootz
0 votes
Imports System
 
Public Class Program
    Public Shared Sub merge_sorted_not_equal_arrays(array1 As Integer(), array2 As Integer())
        Dim size1 As Integer = array1.Length
        Dim size2 As Integer = array2.Length
 
        For i As Integer = 0 To size1 - 1
            If array1(i) > array2(0) Then
			
                Dim tmp As Integer = array1(i)
                array1(i) = array2(0)
                array2(0) = tmp
			
                Dim element0 As Integer = array2(0)
                Dim k As Integer = 1

                While k < size2 AndAlso array2(k) < element0
                    array2(k - 1) = array2(k)
                    k += 1
                End While
 
                array2(k - 1) = element0
            End If
        Next
    End Sub
 
    Public Shared Sub Main(ByVal args As String())
        Dim array1 As Integer() = New Integer() {1, 4, 6, 8, 10}
        Dim array2 As Integer() = New Integer() {2, 3, 5, 9}
     
        merge_sorted_not_equal_arrays(array1, array2)
     
        Console.WriteLine(String.Join(" ", array1))
        Console.WriteLine(String.Join(" ", array2))
    End Sub
End Class
 
         
         
         
' run:
'
' 1 2 3 4 5
' 6 8 9 10
'

 



answered Sep 16, 2023 by avibootz
...