C#将数组向右移

时间:2018-09-23 10:02:23

标签: c# arrays

所以我有一个问题,我确实成功地向左移动,但是我有向右移动的问题 这就是我向左移动的方式:

示例我的意思是向左和向右移动

  • 您有一个数组:1,2,3,4,5,
  • 向左移动一个:2、3、4、5、1
  • 右移一档:5、1、2、3、4,

4 个答案:

答案 0 :(得分:1)

向右移动非常相似,因此向左移动,您只需要保存 last 元素而不是 first 一个元素,然后从末尾开始进行迭代:

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

答案 1 :(得分:0)

没有测试,但是应该可以工作:

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

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

答案 2 :(得分:0)

Array.Copybuffer.BlockCopy在大多数情况下可能是最快的方法

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

基准

----------------------------------------------------------------------------
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
----------------------------------------------------------------------------

结果

--- 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 % |
------------------------------------------------------------------------------

代码

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;
   }
}

答案 3 :(得分:0)

最简单的方法:

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