Merge

方案一:
class Merge
{
    public static void main(String[] args)
    {
        int[] a = new int[]{3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
        int[] temp = new int[a.length];
        int mid = (a.length-1)>>1;//利用a.length-1主要是考虑了a.length为奇数和偶数时计算结果是相同的
        int left = 0;//数组的开始的Index
        int right = a.length-1;//数组结束的Index
        int leftIndex = 0;//i用来表示数组a左半边元素的index
        int rightIndex = mid+1;//用rightIndex表示右半部分的元素的Index
        int tempIndex = 0;//表示临时数组的元素;
        while ((leftIndex<=mid)&&(rightIndex<=right))
        {
            if (a[leftIndex]<a[rightIndex])
            {
                temp[tempIndex] = a[leftIndex];
                leftIndex++;
            }
            else
            {
                temp[tempIndex] = a[rightIndex];
                rightIndex++;
            }
            tempIndex++;
        }
        if (leftIndex>mid)
        {
            for (;rightIndex<=right ;rightIndex++,tempIndex++ )
            {
                temp[tempIndex] = a[rightIndex];
            }
        }
        else 
        {
            for (;leftIndex<=mid;leftIndex++,tempIndex++ )
            {
                temp[tempIndex] = a[leftIndex];
            }
        }
        //a = temp;
        for (int l = 0;l<temp.length ;l++ )
        {
            if (l==0)
            {
                System.out.print("{"+temp[l]+",");
            }
            else if (l<temp.length-1)
            {
                System.out.print(temp[l]+",");
            }
            else
                System.out.println(temp[l]+"}");
        }

    }
}
方案二:
class Merge 
{
    public static void main(String[] args) 
    {
        int[] a = new int[]{3,44,38,5,47,15,36,26};
        int[] b = new int[]{27,2,46,4,19,50,48};
        int[] c = new int[a.length+b.length];
        int i=0,j=0,t=0;

        while ((i<a.length)&&(j<b.length))
        {
            if (a[i]<b[j])
            {
                c[t]=a[i];
                i++;
            }
            else 
            {
                c[t] = b[j];
                j++;
            }
            t++;
        }
        if (i==a.length)
        {
            for (;j<b.length;j++,t++ )
            {
                c[t]=b[j];
            }
        }
        else 
        {
            for (;i<b.length ;i++,t++ )
            {
                c[t]=a[i];
            }
        }
        for (int l = 0;l<c.length ;l++ )
        {
            if (l==0)
            {
                System.out.print("{"+c[l]+",");
            }
            else if (l<c.length-1)
            {
                System.out.print(c[l]+",");
            }
            else
                System.out.println(c[l]+"}");
        }
    }
}*/

这里写图片描述

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、下4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值