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] + " ");
}
}
}
两个数组按大小顺序排列
最新推荐文章于 2023-09-19 14:34:53 发布