[转载]:合并两个已排序好的int数组,并排序返回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;
??????? }
總結
以上是生活随笔為你收集整理的[转载]:合并两个已排序好的int数组,并排序返回c#实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Can't connect to MyS
- 下一篇: [C#]我自己写的一个对字节中每位进行修