zoukankan      html  css  js  c++  java
  • [转载]:合并两个已排序好的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;
            }

     
    做个快乐的自己。
  • 相关阅读:
    PHP、JAVA、C#、Object-C 通用的DES加密
    xtraScrollableControl 滚动条随鼠标滚动
    C#让TopMost窗体弹出并置顶层但不获取当前输入焦点的终极办法
    C#获取“所有用户桌面”的路径
    C#如何获取快捷方式指向的目标文件
    10
    09
    新浪微博中tableview中头部信息
    ASIHTTPRequest类库简介和使用说明
    IOS常用设计模式之委托模式
  • 原文地址:https://www.cnblogs.com/Jessy/p/2114231.html
Copyright © 2011-2022 走看看