首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

归并已排序的数组(java)

2012-08-30 
合并已排序的数组(java)int[] a { 1, 3, 5, 7, 9,11 ,12}int[] b { 2, 3, 4, 6, 8, 10,33 }int[] c

合并已排序的数组(java)

  int[] a = { 1, 3, 5, 7, 9,11 ,12};        int[] b = { 2, 3, 4, 6, 8, 10,33 };        int[] c=new int[a.length+b.length];        int temp=0;        int aindex = 0;        int bindex = 0;        while (aindex < a.length && bindex < b.length) {            if (a[aindex] == b[bindex]) {                System.out.print(a[aindex]+" "+b[bindex]+" ");                c[temp++]=a[aindex];                c[temp++]=b[bindex];                aindex++;                bindex++;            } else if (a[aindex] < b[bindex]) {                System.out.print(a[aindex]+" ");                c[temp++]=a[aindex];                aindex++;            } else {                System.out.print(b[bindex] + " ");                 c[temp++]=b[bindex];                bindex++;            }        }        while (aindex<a.length){            System.out.print(a[aindex] + " ");              c[temp++]=a[aindex];            aindex++;        }        while (bindex<b.length){            System.out.print(b[aindex] + " ");              c[temp++]=b[bindex];            bindex++;        }        System.out.println("\n"+Arrays.toString(c));

热点排行