算法:给定两个已从小到大排好序的整型数组arrA和arrB,将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序...

namespace Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * 给定两个已从小到大排好序的整型数组arrA和arrB
             * 将两个数组合并成arrC,使得arrC也要按从小到大的顺序排好序
             */
            int[] arrA = new int[] { 1, 3, 5, 9, 10 };
            int[] arrB = new int[] { 2, 4, 6, 7, 8 };

            int[] arrC = new int[arrA.Length + arrB.Length];
            int k = 0;

            int m = 0;
            int n = 0;

            for (int i = m; i < arrA.Length; i++)
            {
                for (int j = n; j < arrB.Length; j++)
                {
                    if (arrA[i] < arrB[j])
                    {
                        arrC[k++] = arrA[i];
                        m++;
                        n = j;
                        if (i == arrA.Length - 1)
                        {
                            for (int x = j; x < arrB.Length; x++)
                            {
                                arrC[k++] = arrB[x];
                            }
                        }
                        break;
                    }
                    else
                    {
                        arrC[k++] = arrB[j];
                        m = i;
                        n++;
                        if (j == arrB.Length - 1)
                        {
                            for (int x = i; x < arrA.Length; x++)
                            {
                                arrC[k++] = arrA[x];
                            }
                        }
                        continue;
                    }
                }
            }

            for (int i = 0; i < arrC.Length; i++)
            {
                Console.WriteLine(arrC[i]);
            }
            Console.Read();
        }
    }
}

转载于:https://www.cnblogs.com/myself/archive/2010/07/07/1772727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值