שתף באמצעות


Swapping Items of an Array

Question

Wednesday, January 6, 2016 3:45 PM

How I can swap items of an array as follow  if I have this array { 1,1,0,2,1,1,2}  and I want to get this{ 2,1,1,2,0,1,1} and if j or I become greater than s2.count the there is no swap  …  the code below   produces only  { 2,1,1,2,1 ,1,1} when flip value is 2  I mean swap only   first and third element   

Public Function SearchArea(ByVal s() As Double, ByVal k As Integer, ByVal flip As Integer, ByVal n As Integer) As Double()
Dim s2() As Double
        Dim j As Integer = 0
        Dim i As Integer = 0
        k = 1       
        s2 = s.Clone()
        While i < 3
            j = k + (i * flip)
            If j < s2.Count Then
                i = (j + flip) - 1
                If i <= s2.Count Then
                    Dim temp As Object = s2(i + 1)
                    s2(i) = s2(j - 1)
                    s2(j - 1) = temp
                Else
                    s2(i) = s2(i)
                End If
                i = i + 1
                i = i - flip
            Else
                j = i
            End If
        End While

        
        Return s2
    End Function

All replies (11)

Saturday, January 9, 2016 11:38 AM ✅Answered

The simplest, once you know which position to swap, is to build a sub to perform the switch and use something like this:

 Dim intTemp As Integer

 intTemp = MyArray(IndexFrom)
 MyArray(IndexFrom) = MyArray(IndexTo)
 MyArray(IndexTo) = intTemp

Here is the short example I made

And the supporting code:

Public Class Form1
    Private MyArray As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        'Set the label to display the array
        DisplayArrayInLabel(MyArray, Label1, ";")
        'Clear the result label
        Label2.Text = ""
    End Sub
    Private Sub DisplayArrayInLabel(ByRef ThisArray As Integer(), ByRef ThisLabel As Label, Optional ByVal ThisSeparator As String = ",")
        Dim intIndex As Integer
        ThisLabel.Text = ""
        For intIndex = 0 To ThisArray.Count - 1
            ThisLabel.Text &= ThisArray.GetValue(intIndex).ToString
            If intIndex < ThisArray.Count - 1 Then
                ThisLabel.Text &= ","
            End If
        Next
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim SwitchFrom As Integer
        Dim SwitchTo As Integer

        'Test if position values are numeric and are valid
        If IsNumeric(TextBox1.Text) = True And IsNumeric(TextBox2.Text) = True Then
            SwitchFrom = CInt(TextBox1.Text)
            SwitchTo = CInt(TextBox2.Text)
            'Check if valid positions
            If SwitchFrom >= 0 And SwitchFrom < MyArray.Count - 1 And SwitchTo >= 0 And SwitchTo <= MyArray.Count - 1 Then
                Switch4Index(SwitchFrom, SwitchTo)
                'Display result
                DisplayArrayInLabel(MyArray, Label2, ";")
            Else
                MessageBox.Show("One position value is out of bounds")
            End If
        Else
            MessageBox.Show("Position value is not numeric")
        End If

    End Sub

    Private Sub Switch4Index(ByVal IndexFrom As Integer, ByVal IndexTo As Integer)
        Dim intTemp As Integer
        intTemp = MyArray(IndexFrom)
        MyArray(IndexFrom) = MyArray(IndexTo)
        MyArray(IndexTo) = intTemp

    End Sub
End Class

To my knowledge there are no array internal methods to swap elements... you always need to store one value before swapping...

Actually now I get interested in seeing if one can build an internal to integer()... that's another question, but it would be something like MyArray.Swap(Index1,Index2)....

And after searching...it is called Extensions...look here:https://social.msdn.microsoft.com/Forums/vstudio/en-US/44772250-a929-4527-86fc-cc8194539c82/is-there-swap-function-in-vbnet?forum=vbgeneral

Cyrille Precetti
Bonne Année! Happy New Year!


Wednesday, January 6, 2016 4:18 PM

You can use the Enumerable.Reverse method to create a new array with elements in reverse order.

Dim oldArray() As Integer = {1, 1, 0, 2, 1, 1, 2}
Dim newArray() As Integer = oldArray.Reverse.ToArray

Or you can use Array.Reverse to reverse the order of the elements in an array without creating a new one.

Dim oldArray() As Integer = {1, 1, 0, 2, 1, 1, 2}
Array.Reverse(oldArray)

Wednesday, January 6, 2016 4:28 PM

 I need to swap elements according to specific index not reverse them


Wednesday, January 6, 2016 4:32 PM

 I need to swap elements according to specific index not reverse them

Then please explain the rules. The code you posted is rather strange. Among other things:

  • The function has an argument called 'k', but you ignore the value of k and initialise it to 1.
  • The function has an argument called 'n' that is never used.
  • You have not explained what value 'flip' should have or what its effect is supposed to be.

Wednesday, January 6, 2016 5:01 PM

ok  I'm working on  bees colony algorithm and this function just a trial ,so N is the same as s2.count in this code is 7 , and  k is number of bees and here I have 3 so each bee will change the vector according to it's number  for example here I'm working with the first bee so k is 1 


Wednesday, January 6, 2016 6:03 PM

ok  I'm working on  bees colony algorithm and this function just a trial ,so N is the same as s2.count in this code is 7 , and  k is number of bees and here I have 3 so each bee will change the vector according to it's number  for example here I'm working with the first bee so k is 1 

Please explain what the function is supposed to do in a way that will make sense to someone (like me) who has no idea what the bees colony algorithm is. 


Saturday, January 9, 2016 10:22 AM

OK ,for example I need to put value of position 3 in 1 and put value of 1 in 2 ,then put value of position 5 in 2 and 2 in position 4 


Saturday, January 9, 2016 11:58 AM

Here I played with the <extension>....

so you can define your own swap function as a method to integer():

Module IntegerArrayExtensions
    <Extension()>
    Sub Swap2Index(ByVal OneArray As Integer(), ByVal Index1 As Integer, ByVal Index2 As Integer)
        Dim tempIndex As Integer
        tempIndex = OneArray(Index1)
        OneArray(Index1) = OneArray(Index2)
        OneArray(Index2) = tempIndex
    End Sub

End Module

and then simply call: MyArray.Swap2Index(SwitchFrom,SwitchTo)

Now there should be some checks on validity but it works reasonably as an example.

Cyrille Precetti
Bonne Année! Happy New Year!


Saturday, January 9, 2016 1:33 PM

 I need to swap elements according to specific index not reverse them

If I understand this correctly you want to reverse a certain portion of an existing array.  Array.Reverse can do that if you know, or can know, the start and length of the portion.  Here is a test

        Dim beeA() As Integer = {9, 8, 1, 1, 0, 2, 1, 1, 2, 9, 7}
        Array.Reverse(beeA, 2, 7)

This is the sample you gave surrounded by 9,8 and 9,7.

"Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.


Saturday, January 9, 2016 5:13 PM

Cyrille Precetti  Thanks too much it's really help 


Saturday, January 9, 2016 5:15 PM

dbasnett your solution is a good idea for my algorithm thanks alot