4

so I have a problem I did shifting to left successfully but I have problems with shifting to right This is how I shift to the left:

Example what I mean shift to left and right:

  • you have an array: 1, 2, 3, 4, 5,
  • shift one to left: 2, 3, 4, 5, 1,
  • shift one to right: 5, 1, 2, 3, 4,
1

4 Answers 4

5

Array.Copy and buffer.BlockCopy will probably be the fastest approach in most situations

var temp = ary[ary.Length - 1];
Array.Copy(ary,0, ary,1, ary.Length-1);
ary[0] = temp;

Benchmarks

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description      : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3401 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

Results

--- Standard input -----------------------------------------------------------
| Value     |    Average |    Fastest |    Cycles | Garbage | Test |    Gain |
--- Scale 1,000 ----------------------------------------------- Time 0.006 ---
| BlockCopy |  39.130 ns |   0.000 ns |   1.354 K | 0.000 B | N/A  | 95.65 % |
| ArrayCopy | 184.615 ns |   0.000 ns |   1.831 K | 0.000 B | N/A  | 79.49 % |
| Unsafe    | 594.828 ns | 300.000 ns |   3.245 K | 0.000 B | N/A  | 33.91 % |
| Index     | 900.000 ns | 900.000 ns |   4.145 K | 0.000 B | Base |  0.00 % |
--- Scale 10,000 ---------------------------------------------- Time 0.002 ---
| BlockCopy | 417.857 ns | 300.000 ns |   2.704 K | 0.000 B | N/A  | 95.26 % |
| ArrayCopy |   1.948 µs |   1.801 µs |   8.065 K | 0.000 B | N/A  | 77.92 % |
| Unsafe    |   6.377 µs |   5.703 µs |  23.116 K | 0.000 B | N/A  | 27.72 % |
| Index     |   8.822 µs |   8.705 µs |  31.689 K | 0.000 B | Base |  0.00 % |
--- Scale 100,000 --------------------------------------------- Time 0.020 ---
| BlockCopy |   4.814 µs |   4.803 µs |  17.882 K | 0.000 B | N/A  | 94.61 % |
| ArrayCopy |  25.016 µs |  24.015 µs |  87.177 K | 0.000 B | N/A  | 71.99 % |
| Unsafe    |  62.844 µs |  58.537 µs | 216.297 K | 0.000 B | N/A  | 29.63 % |
| Index     |  89.307 µs |  82.253 µs | 306.309 K | 0.000 B | Base |  0.00 % |
--- Scale 1,000,000 ------------------------------------------- Time 0.216 ---
| BlockCopy |  63.048 µs |  60.639 µs | 217.764 K | 0.000 B | N/A  | 93.22 % |
| ArrayCopy | 257.222 µs | 234.751 µs | 880.436 K | 0.000 B | N/A  | 72.33 % |
| Unsafe    | 653.073 µs | 604.590 µs |   2.230 M | 0.000 B | N/A  | 29.74 % |
| Index     | 929.520 µs | 843.845 µs |   3.173 M | 0.000 B | Base |  0.00 % |
--- Scale 10,000,000 ------------------------------------------ Time 2.346 ---
| BlockCopy | 998.789 µs | 924.597 µs |   3.414 M | 0.000 B | N/A  | 89.70 % |
| ArrayCopy |   4.875 ms |   4.563 ms |  16.575 M | 0.000 B | N/A  | 49.74 % |
| Unsafe    |   7.225 ms |   6.922 ms |  24.626 M | 0.000 B | N/A  | 25.51 % |
| Index     |   9.699 ms |   9.313 ms |  33.063 M | 0.000 B | Base |  0.00 % |
--- Scale 100,000,000 ---------------------------------------- Time 23.824 ---
| BlockCopy |  11.989 ms |  11.528 ms |  40.759 M | 0.000 B | N/A  | 87.67 % |
| ArrayCopy |  49.423 ms |  46.981 ms | 167.664 M | 0.000 B | N/A  | 49.18 % |
| Unsafe    |  75.024 ms |  71.877 ms | 255.297 M | 0.000 B | N/A  | 22.86 % |
| Index     |  97.254 ms |  95.442 ms | 331.134 M | 0.000 B | Base |  0.00 % |
------------------------------------------------------------------------------

Code

public class Index : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var t = Input[Input.Length - 1];
      for (var i = Input.Length - 1; i > 0; i--)
         Input[i] = Input[i - 1];
      Input[0] = t;
      return Input;
   }
}

public class Unsafe : WorkLoad<int[], int[]>
{
   public override unsafe int[] Run()
   {
      fixed (int* p = Input)
      {
         var t = *(p + (Input.Length - 1));
         for (var i = Input.Length - 1; i > 0; i--)
            *(p + i) = *(p + (i - 1));
         *p = t;
      }
      return Input;
   }
}

public class ArrayCopy : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var temp = Input[Input.Length - 1];
      Array.Copy(Input, 0, Input, 1, Input.Length - 1);
      Input[0] = temp;
      return Input;
   }
}

public class BlockCopy : WorkLoad<int[], int[]>
{
   public override int[] Run()
   {
      var temp = Input[Input.Length - 1];
      Buffer.BlockCopy(Input, 0, Input, 1, Input.Length - 1);
      Input[0] = temp;
      return Input;
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What tool do you use to benchmark code like that? it looks very useful
@JavierCapello is a custom tool i wrote for my specific use cases i use at work and at home, however you can use benchmarkDotNet
3

Shifting to the right is quite similar so shifting to the left, you just need to save the last element instead of the first one, and iterate from the end backwards:

int t = tab[tab.Length - 1];
for (int i = tab.Length - 1; i > 0; i--)
    tab[i] = tab[i - 1];
tab[0] = t;

2 Comments

After that my array looks like this: 5, 1, 1, 1, 1,
@kubn2 damn, that was dumb of me. You need to also reverse the order of iteration - see my edited answer.
0

Didn't test it but it should work:

int t = tab[tab.Length-1];

for (int i = 0; i < tab.Length; i++){
    int t2= tab[i];
    tab[i] = t;
    t = t2;
 }

Comments

0

The easiest way to go:

//right shift
        int[] tmp = new int[tab.Length];
        for (int i = 0; i < tab.Length; i++)
        {
            tmp[(i + 1) % tmp.Length] = tab[i];
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.