[Solved] How to change position of an element in an array?


If you can do an increment of one, just repeat that the required number of times.

A better way would be: (let’s call the increment n); store the end n elements in another array; move the elements before that towards the end of the array; copy back the elements from the other array to the start. Like this:

Module Module1

    Sub Main()
        Dim players() As String = {"Adam", "Becky", "Clive", "Debby", "Edward", "Fiona"}
        Dim rotateBy = 2

        ' make sure shiftBy is in a usable range
        rotateBy = rotateBy Mod players.Length
        ' if shiftBy is negative, make it positive such that a left-rotate is performed
        If rotateBy < 0 Then
            rotateBy = rotateBy + players.Length
        End If

        ' store the elements which will be moved to the other end of the array
        Dim tmp(rotateBy - 1) As String
        Dim startIndex = players.Length - rotateBy
        For i = startIndex To players.Length - 1
            tmp(i - startIndex) = players(i)
        Next

        ' move the elements
        For i = players.Length - 1 - rotateBy To 0 Step -1
            players(i + rotateBy) = players(i)
        Next

        'fill in the other elements
        For i = 0 To rotateBy - 1
            players(i) = tmp(i)
        Next

        ' show the result
        Console.WriteLine(String.Join(", ", players))

        Console.ReadLine()

    End Sub

End Module

Note that the copying of elements within the players array is done backwards so that an overlapping range does not splat over values which have yet to be moved.

To shift the elements backwards, use a negative value for rotateBy.

If you are using a language with a function which gives array copying functions, this method is easy to adapt to use that functionality. For anyone wondering about that in the future:

Module Module1

    Sub Main()
        Dim players() As String = {"Adam", "Becky", "Clive", "Debby", "Edward", "Fiona"}
        Dim rotateBy = 4

        ' make sure shiftBy is in a usable range
        rotateBy = rotateBy Mod players.Length
        ' if shiftBy is negative, make it positive such that a left-rotate is performed
        If rotateBy < 0 Then
            rotateBy = rotateBy + players.Length
        End If

        ' store the elements which will be moved to the other end of the array
        Dim tmp(rotateBy - 1) As String
        Dim startIndex = players.Length - rotateBy

        Array.Copy(players, startIndex, tmp, 0, rotateBy)
        Array.Copy(players, 0, players, rotateBy, startIndex)
        Array.Copy(tmp, players, tmp.Length)

        ' show the result
        Console.WriteLine(String.Join(", ", players))

        Console.ReadLine()

    End Sub

End Module

As you mentioned any programming language, this is what it could be in C# as a function but without using any framework methods such as Array.Copy:

public static void RotateUsingLoops<T>(T[] elements, int rotateBy)
{
    rotateBy = rotateBy % elements.Length;
    if (rotateBy < 0)
    {
        rotateBy += elements.Length;
    }
    T[] tmp = new T[rotateBy];
    int startIndex = elements.Length - rotateBy;

    // store the elements which will be moved to the other end of the array
    for (int i = startIndex; i < elements.Length; i++)
    {
        tmp[i - startIndex] = elements[i];
    }

    // move the elements
    for (int i = elements.Length - 1 - rotateBy; i >= 0; i--)
    {
        elements[i + rotateBy] = elements[i];
    }

    // fill in the other elements
    for (int i = 0; i < rotateBy; i++)
    {
        elements[i] = tmp[i];
    }
}

Whereby you could use

static void Main(string[] args)
{
    var a = new string[] { "A", "B", "C", "D" };
    RotateUsingLoops(a, 2);
    Console.WriteLine(string.Join(", ", a));

    Console.ReadLine();

}

to get an output of:

C, D, A, B

0

solved How to change position of an element in an array?