Merge
来源:互联网 发布:英文版进销存软件 编辑:程序博客网 时间:2024/06/03 03:30
方案一: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]+"}"); } }}*/
阅读全文
0 0
- merge
- merge
- MERGE
- merge
- Merge
- MERGE
- merge
- merge
- merge
- Merge
- merge
- Merge
- Merge
- Merge
- merge
- MERGE
- Merge
- merge
- Java9 新特性速览
- 内存泄漏/溢出/垃圾回收
- 文件输入输出流的简单原理---fileoutputstream/fileinputstream
- php 返回数组中指定多列的方法
- 使用过的vue组件记录
- Merge
- buntu--apt-get update(由于没有公钥,无法验证下列签名)
- Kali linux渗透测试(六)
- LinkedList源码详解
- iOS APP运行奔溃及解决办法总结<待更新>
- 删除字符串中的一个字符有重复的也删除掉
- [NOIP模拟] 证明 proof
- 东方幻想乡系列模拟赛T2琪露诺题解
- 将Medium中的博客导出成markdown