• [转载]:合并两个已排序好的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;
            }

     
    做个快乐的自己。
  • 相关阅读:
    maven_常用命令
    tomcat_server.xml常用配置
    tomcat线程池原理
    Tomcat处理HTTP请求原理&线程池
    javaweb-javaScript(一)
    javaweb-html快速入门
    javaweb-css
    javaweb-单元测试与注解
    javaweb-文件的上传下载
    javaweb-jsp开发模式+JSON插件
  • 原文地址:https://www.cnblogs.com/Jessy/p/2114231.html
Copyright © 2020-2023  润新知