两个有序数组合并算法

有两个有序数组A和B,如果把A和B合并起来到C中,具体算法如下:

public static int [] MergeArray(int[] ArrayLeft, int[] ArrayRight)//
{
  int length = ArrayLeft.Length + ArrayRight.Length;
  int[] ArrayMerge = new int[length];
  int i = 0;
  int j = 0;
  while (i < ArrayLeft.Length && j < ArrayRight.Length)
  {
    if (ArrayLeft[i] > ArrayRight[j])
    {
      ArrayMerge[i + j] = ArrayRight[j];
      j++;
    }
    else
    {
      ArrayMerge[i + j] = ArrayLeft[i];
      i++;
    }
  }
  while (i < ArrayLeft.Length)
  {
    ArrayMerge[i + j] = ArrayLeft[i];
    i++;
  }
  while (j < ArrayRight.Length)
  {
    ArrayMerge[i + j] = ArrayRight[j];
    j++;
  }
  return ArrayMerge;

}

posted on 2016-03-02 17:36  shaomine  阅读(513)  评论(0编辑  收藏  举报