/// <summary> /// 两个从小到大排序好的int数组,合并后也返回一个从小到大排序好的数组, /// 包含两个数组中全部的元素 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static int[] MergeArray(int[] a, int[] b) { if( a == null || b== null ) throw new NotSupportedException();
int lena = a.Length; int lenb = b.Length; int[] c = new int[lena+lenb];
int i, j, n; i = j = n = 0;
while (i < lena && j < lenb) { if (a[i] < b[j]) { c[n++] = a[i++]; } else if (a[i] > b[j]) { c[n++] = b[j++]; } else { c[n++] = a[i++]; c[n++] = b[j++]; } }
if (i == lena) { while (j < lenb) c[n++] = b[j++]; } else { while (i < lena) c[n++] = a[i++]; }
return c; }
/// <summary> /// 两个从小到大排序好的int数组,合并后也返回一个从小到大排序好的数组, /// 包含两个数组中全部的元素 /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> public static int[] MergeArray(int[] a, int[] b) { if( a == null || b== null ) throw new NotSupportedException();
int lena = a.Length; int lenb = b.Length; int[] c = new int[lena+lenb];
int i, j, n; i = j = n = 0;
while (i < lena && j < lenb) { if (a[i] < b[j]) { c[n++] = a[i++]; } else if (a[i] > b[j]) { c[n++] = b[j++]; } else { c[n++] = a[i++]; c[n++] = b[j++]; } }
if (i == lena) { while (j < lenb) c[n++] = b[j++]; } else { while (i < lena) c[n++] = a[i++]; }
return c; }
|
请发表评论