两个数组按大小顺序排列

package com.zz.sort;

public class Compare2Arrag {
	/**
	 * 两个数组按大小顺序排列
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		//数组一和数组二必须是排序好的
		int[] oneArrags = new int[]{1,3,5,7,9};
		int[] twoArrags = new int[]{2,3,6,8};
		
		int length = oneArrags.length + twoArrags.length ;
		int left = 0;
		int rightStart = 0;
		int tempIndex = 0;
		//int middle =  oneArrags.length ;
		//int leftEnd = middle - 1;
		//int rightStart = middle;
		//int right = length -1;
		int[] tempArrags = new int[length];
		
		while (left < oneArrags.length && rightStart < twoArrags.length) {
			//比较值小的放入临时数据
			if (oneArrags[left] < twoArrags[rightStart]) {
				tempArrags[tempIndex++] = oneArrags[left++]; 
			} else {
				tempArrags[tempIndex++] = twoArrags[rightStart++];
			}
		}
		
		//数组一没有结束的放入临时数组
		while(left < oneArrags.length) {
			tempArrags[tempIndex++] = oneArrags[left++]; 
		}
		
		//数组二没有结束的放入临时数组
		while(rightStart < twoArrags.length) {
			tempArrags[tempIndex++] = twoArrags[rightStart++];
		}
		
		for(int i = 0; i < tempArrags.length; i++ ) {
			System.out.print(tempArrags[i] + " ");
		}
	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值