Artemkller545
Artemkller545

Reputation: 999

shifting array elements to right?

I can't use a built-in function for this, I must use my own logic.

I've done element shifting to the left side, but the right side doesn't work for me. Not sure why.

My method for left:

public int[] shiftLeft(int[] arr) {
    int[] demo = new int[arr.length];
    int index = 0;
    for (int i = 0; i < arr.length - 1; i++) {
        demo[index] = arr[i + 1];
        index++;
    }
    return demo;
}

and my attempt for the right shifting:

public int[] shiftRight(int[] arr) {
    int[] demo = new int[arr.length];
    int index = 0;
    for (int i = arr.length - 1; i >= 0; i--) {
        demo[index] = arr[(i - 1 > 0) ? i - 1 : 0];
        index++;
    }
    return demo;
}

What am I doing wrong?

By shifting I mean:

you have an array, 1 2 3 4 5 6
Shifting it to left by one: 2 3 4 5 6 1
Shifting it to right by one: 6 1 2 3 4 5

Upvotes: 15

Views: 35274

Answers (11)

Baqer Naqvi
Baqer Naqvi

Reputation: 6504

Without using external array;

public static int[] right(int[] A)
{
     var tempo = A[0];
     for(var i=0; i<A.Length-1; i++)
     {
         var yolo = A[i + 1];
         A[i + 1] = tempo;
         tempo = yolo;
     }
     A[0] = tempo;
     return A;
 }

public static int[] left(int[] A)
{
    var tempo = A[A.Length - 1];
    for (var i = A.Length - 1; i >0; i--)
    {
          var yolo = A[i - 1];
          A[i -1] = tempo;
          tempo = yolo;
    }
    A[A.Length - 1] = tempo;
    return A;
}

Upvotes: 0

lumpus
lumpus

Reputation: 1

maybe this works for anyone seeing this post :

private int[] shiftLinear(int[] linArray, int shift){   
    int length = linArray.Length;
    int[] shifted = new int[length];
    shift = shift % length;
    if (shift >= 0) {
        for (int n = shift ; n < length; n++) shifted[n] = linArray[n-shift];   
        if (shift != 0)  for (int n = 0; n < shift; n++) shifted[n] = linArray[length-1-n]; 
    } else {
        for (int n = 0 ; n < length+shift; n++) shifted[n] = linArray[n-shift];   
        for (int n = length+shift; n < length ; n++) shifted[n] = linArray[n-length-shift]; 
    }
    return shifted;
}

Upvotes: 0

Nomit
Nomit

Reputation: 115

Usually I use this code. You can rewrites to the array extension method.

public static T[] Shift<T>(T[] array, int shiftValue)
{       
    var newArray = new T[array.Length];
    shiftValue -= array.Length;
    if(shiftValue < 0)
    {
        shiftValue*=-1;
    }


    for(var i=0; i<array.Length; i++)
    {
        var index = (i + shiftValue) % array.Length;

        newArray[i] = array[index]; 
    }
    return newArray;
}

Upvotes: 0

Konrad Morawski
Konrad Morawski

Reputation: 8394

LINQ solution, just to add some diversity.

static int[] LeftShift(int[] array)
{            
    // all elements except for the first one... and at the end, the first one. to array.
    return array.Skip(1).Concat(array.Take(1)).ToArray();
}

static int[] RightShift(int[] array)
{
    // the last element (because we're skipping all but one)... then all but the last one.
    return array.Skip(array.Length - 1).Concat(array.Take(array.Length - 1)).ToArray();
}

Probably not recommended if performance matters (for large arrays).

I realize that the OP is not supposed to use a "built-in function".

Upvotes: 4

Asegid Debebe
Asegid Debebe

Reputation: 165

  public static int[] shiftRight(int[] arr){

      int[] demo = new int[arr.Length];

      for (int i = 0; i <= arr.Length - 2; i++)
      {
          demo[i + 1] = arr[i];
      }

      demo[0] = arr[arr.Length - 1];
      return demo;
  }

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

The easiest way to go:

public int[] shiftLeft(int[] arr) 
{
    int[] demo = new int[arr.Length];

    for (int i = 0; i < arr.Length - 1; i++) 
    {
        demo[i] = arr[i + 1];
    }

    demo[demo.Length - 1] = arr[0];

    return demo;
}

public int[] shiftRight(int[] arr) 
{
    int[] demo = new int[arr.Length];

    for (int i = 1; i < arr.Length; i++) 
    {
        demo[i] = arr[i - 1];
    }

    demo[0] = arr[demo.Length - 1];

    return demo;
}

Upvotes: 14

Frank Pfattheicher
Frank Pfattheicher

Reputation: 432

Use Arra.Copy...

public int[] shiftLeft(int[] arr) {
var result = new int[arr.Length];
Array.Copy(arr, 1, result, 0, arr.Length - 1);
result[arr.Length - 1] = arr[0];
return result;

}

    public int[] shiftRight(int[] arr) {
  var result = new int[arr.Length];
  Array.Copy(arr, 0, result, 1, arr.Length - 1);
  result[0] = arr[arr.Length - 1];
  return result;
}

Upvotes: 0

Harrison
Harrison

Reputation: 3953

public int[] shiftRight(int[] arr) 
{
    int[] demo = new int[arr.length];

    Array.Copy(arr,arr.Length-1,demo,0,1); // Copy last position to first
    Array.Copy(arr,0,demo,1,arr.Length-1); // Copy the rest shifted one       

    return demo;
}

Upvotes: -1

rae1
rae1

Reputation: 6134

Try this,

public int[] ShiftRight(int[] arr) 
{
    int[] demo = new int[arr.Length];
    for (int i = 0; i < arr.Length; i++) {
        demo[i] = arr[i == 0 ? (arr.Length - 1) : (i - 1)];
    }
    return demo; 
}

Upvotes: 0

Robert Fricke
Robert Fricke

Reputation: 3643

//right shift with modulus
for (int i = 0; i < arr.length; i++) {
    demo[(i+1) % demo.length] = arr[i];
}

Upvotes: 26

JaredPar
JaredPar

Reputation: 754545

The problem here is that you need to special case the left shift of the first element. For every element but the first the new index of the value will be oldIndex - 1. This is essentially what the loop is doing. However the first element has a new index of oldLength - 1. This needs to be special cased somewhere in the code base.

Upvotes: 0

Related Questions