Detailed explanation of three ways to realize array inversion by C

  • 2021-12-11 18:41:50
  • OfStack

After eating at noon at work today, I strolled around my blog and saw a question: Array reversal, I went home at night and had nothing to do after taking a bath, so I practiced one by myself.


public static class ArrayReserve 
  { 
    /// <summary> 
    ///  Use  Array.Reverse(Arrar)  Reverse all  
    /// </summary> 
    /// <param name="arr"></param> 
    public static void ReverseDemo1(int[] arr) 
    { 
      Console.WriteLine(" Use  Array.Reverse(Arrar)  Reverse all "); 
      Array.Reverse(arr); 
    } 
    /// <summary> 
    ///  Use  Array.Reverse(Array arr,int begin,int end), Invert the specified part  
    /// </summary> 
    /// <param name="arr"></param> 
    /// <param name="begin"></param> 
    /// <param name="end"></param> 
    public static void ReverseDemo2(int[] arr, int begin, int end) 
    { 
      Console.WriteLine(" Use  Array.Reverse(Array arr,int begin,int end), Invert the specified part "); 
      Array.Reverse(arr, begin, end); 
    } 
    /// <summary> 
    ///  Using custom methods to implement inversion  
    /// </summary> 
    /// <param name="arr"></param> 
    /// <param name="begin"></param> 
    /// <param name="end"></param> 
    public static void ReverseDemo3(int[] arr, int begin, int end) 
    { 
      Console.WriteLine(" Using custom methods to implement inversion "); 
      if(null==arr) 
      { 
        throw new ArgumentNullException("arr", "Array Cannot be for null"); 
      } 
      if(begin<=0 || end <=0) 
      { 
        throw new ArgumentOutOfRangeException(" The start or end index is not set correctly "); 
      } 
      if(end>arr.Length) 
      { 
        throw new ArgumentOutOfRangeException("end", " End index exceeds array length "); 
      } 
      while(begin<end) 
      { 
        int temp = arr[end]; 
        arr[end] = arr[begin]; 
        arr[begin] = temp; 
        begin++; 
        end--; 
      } 
    } 
  /// <summary> 
    ///  Using custom methods to implement inversion ( Use stack "last in first out")  
    /// </summary> 
    /// <param name="arr"></param> 
    /// <param name="begin"></param> 
    /// <param name="end"></param> 
    public static void ReverseDemo4(int[] arr, int begin, int end) 
    { 
      Console.WriteLine(" Using custom methods to implement inversion ( Use stack "last in first out") "); 
      if (null == arr) 
      { 
        throw new ArgumentNullException("arr", "Array Cannot be for null"); 
      } 
      if (begin <= 0 || end <= 0) 
      { 
        throw new ArgumentOutOfRangeException(" The start or end index is not set correctly "); 
      } 
      if (end > arr.Length) 
      { 
        throw new ArgumentOutOfRangeException("end", " End index exceeds array length "); 
      } 
      Stack<int> intStack = new Stack<int>(); 
      int tempBegin = begin; 
      for(;begin<=end;begin++) 
      { 
        intStack.Push(arr[begin]); 
      } 
      for (; tempBegin <= end; tempBegin++) 
      { 
        arr[tempBegin] = intStack.Pop(); 
      } 
    } 
  }

Related articles: