C algorithm for adjusting array order so that odd numbers precede even numbers

From , 4 Years ago, written in C#, viewed 55 times.
URL https://pastebin.vip/view/1d3d6cb6
  1. #region 调整数组顺序使奇数位于偶数前面  
  2. /// <summary>  
  3. /// 输入一个 整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分  
  4. /// Reorder中array为待排序的数组,使用组合的方式调用使用何种方法进行排序(如奇数在前,偶数在后,或能被3整除的数在前,不能被3整除的数在后)  
  5. /// </summary>  
  6. class Reorder  
  7. {  
  8.     private List<int> _array;  
  9.     private RecorderOperator _op;  
  10.  
  11.     public List<int> array  
  12.     {  
  13.         get { return _array; }  
  14.         set { _array = value; }  
  15.     }  
  16.     public RecorderOperator op  
  17.     {  
  18.         get { return _op; }  
  19.         set { _op = value; }  
  20.     }  
  21.     public Reorder(List<int> array, RecorderOperator op)  
  22.     {  
  23.         _array = array;  
  24.         _op = op;  
  25.     }  
  26.     public Reorder() { }  
  27.  
  28.     public void ReorderArray()  
  29.     {  
  30.         int length = array.Count;  
  31.         int start = 0, end = length - 1;  
  32.         while (start < end)  
  33.         {  
  34.             while (start < end && !op.Operator(array[start]))  
  35.                 start++;  
  36.             while (start < end && op.Operator(array[end]))  
  37.                 end--;  
  38.             if (start < end)  
  39.             {  
  40.                 int temp = array[start];  
  41.                 array[start] = array[end];  
  42.                 array[end] = temp;  
  43.             }  
  44.         }  
  45.     }  
  46.  
  47.     public void Print()  
  48.     {  
  49.         array.ForEach(a=>Console.Write(a+"  "));  
  50.         Console.WriteLine();  
  51.     }  
  52. }  
  53. class RecorderOperator  
  54. {  
  55.      public virtual bool Operator(int n)  
  56.      {  
  57.          return false;  
  58.      }  
  59. }  
  60. class ConcreteRecorderOperator1:RecorderOperator  
  61. {  
  62.     public override bool  Operator(int n)  
  63.     {  
  64.         return (n & 0x1)==0;  
  65.     }  
  66. }  
  67. class ConcreteRecorderOperator2 : RecorderOperator  
  68. {  
  69.     public override bool Operator(int n)  
  70.     {  
  71.         return n%3!=0;  
  72.     }  
  73. }  
  74. #endregion  
  75. class Test{  
  76.     public void ReorderTest()  
  77.     {  
  78.         RecorderOperator op1 = new ConcreteRecorderOperator1();  
  79.         Reorder reorder = new Reorder(new List<int>{2,3,4,9,5},op1);  
  80.         Console.WriteLine("所有奇数位于数组的前半部分,所有偶数位于数组的后半部分");  
  81.         reorder.Print();  
  82.         reorder.ReorderArray();  
  83.         reorder.Print();  
  84.         RecorderOperator op2 = new ConcreteRecorderOperator2();  
  85.         reorder.op = op2;  
  86.         reorder.ReorderArray();  
  87.         Console.WriteLine("能被3整除的数在前,不能被3整除的数在后");  
  88.         reorder.Print();  
  89.     }  
  90. }  
  91. class Program  
  92. {  
  93.      
  94.     static void Main(string[] args)  
  95.     {  
  96.         Test t = new Test();  
  97.         t.ReorderTest();  
  98.        
  99.     }  
  100. }
  101. //csharp/8492

Reply to "C algorithm for adjusting array order so that odd numbers precede even numbers"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website