有两个数组,其元素都是升序排列, 合并为一个数组,要求新的数组也是升序排列
感觉自己的程序中定义的变量太多了,请各位大侠帮忙优化public class Up { /**
* 有两个数组,其元素都是升序排列
* 合并为一个数组,要求新的数组也是升序排列
*/
public static void main(String[] args) {
int [] a = {1,3,5,7,9,10};
int [] b = {2,4,6,8,11,12,13};
Up m = new Up();
m.getInfo(InsertToC(a,b));
}
public static int[] InsertToC(int [] a,int [] b){
int [] c = new int [a.length + b.length];
int x = 0;
int y = 0 ;
for(int i=0 ; i<a.length ; i++){
for(int j= y ; j<b.length ; j++){
if(a[i]>b[j]){
c[x] = b[j];
x++;
y++;
}
else{
c[x] = a[i];
x++;
break;
}
}
}
while(y < b.length){
c[x] = b[y];
x++;
y++;
}
return c;
}
public void getInfo(int [] c){
for(int i=0 ; i<c.length ; i++){
System.out.print(c[i] + " ");
}
}}
感觉自己的程序中定义的变量太多了,请各位大侠帮忙优化public class Up { /**
* 有两个数组,其元素都是升序排列
* 合并为一个数组,要求新的数组也是升序排列
*/
public static void main(String[] args) {
int [] a = {1,3,5,7,9,10};
int [] b = {2,4,6,8,11,12,13};
Up m = new Up();
m.getInfo(InsertToC(a,b));
}
public static int[] InsertToC(int [] a,int [] b){
int [] c = new int [a.length + b.length];
int x = 0;
int y = 0 ;
for(int i=0 ; i<a.length ; i++){
for(int j= y ; j<b.length ; j++){
if(a[i]>b[j]){
c[x] = b[j];
x++;
y++;
}
else{
c[x] = a[i];
x++;
break;
}
}
}
while(y < b.length){
c[x] = b[y];
x++;
y++;
}
return c;
}
public void getInfo(int [] c){
for(int i=0 ; i<c.length ; i++){
System.out.print(c[i] + " ");
}
}}
自己写了一下public class Ascending { /**
* @param args
*/
public int[] ascend(int[] a,int[] b) {
//throws ArrayIndexOutOfBoundsException
int x = 0; //数组a的下标
int y = 0; //数组b的下标
int z = 0; //数组c的下标
int lena = a.length; //a的长度
int lenb = b.length; //b的长度
int[] c = new int[lena + lenb];
// 因为a`b均是升序排列的数组
//while是分别从a`b 中取出较小值,并移动数组下标
while( x < lena && y < lenb ){
if( a[x] <= b[y] ){
c[z] = a[x];
x++ ;
}
else{
c[z] = b[y];
y++ ;
}
z++ ;
}
// 若a中数据未添加完
while(x < lena){
c[z] = a[x];
x++ ;
z++ ;
}
//若b中数据未添加完
while(y < lenb){
c[z] = b[y];
y++ ;
z++ ;
}
return c;
}
// 输出数组C
void print(int[] x){
int len = x.length;
for(int i = 0;i < len;i++){
System.out.print(x[i] + "-");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = {1,3,5,7,9,10};
int[] b = {2,4,6,8,11,12,13};
Ascending asc = new Ascending();
asc.print(asc.ascend(a, b));
}
}
import java.util.Arrays;public class Up { /**
* 有两个数组,其元素都是升序排列
* 合并为一个数组,要求新的数组也是升序排列
*/
public static void main(String[] args) {
int [] a = {1,3,5,7,9,10};
int [] b = {2,4,6,8,11,12,13};
int [] c=new int[a.length+b.length];
for(int i=0;i<a.length;i++){
c[i]=a[i];
}
for(int i=0;i<b.length;i++){
c[i+a.length]=b[i];
}
Arrays.sort(c);
for(int i=0;i<c.length;i++){
System.out.println(c[i]);
}
}
}
目前只学了基本语法和面向对象的一些东西不过刚才查了下APIsortpublic static void sort(int[] a) Sorts the specified array of ints into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance. Parameters:
a - the array to be sorted发现这的确是好方法啊,当然,老师留这个题给我们,目的是让我们自己实现这个过程,谢谢2楼所给出的方法,对7楼大侠所提出的程序也很感谢的,因为让我学到了一种方法,很受用,谢谢,谢谢大家,哈
int[] a = { 1, 3, 5, 7, 9, 10 };
int[] b = { 2, 4, 6, 8, 11, 12, 13 };
int[] c = mergeArray(a, b);
printArray(c);
} public static int[] mergeArray(int[] a, int[] b) {
int[] c = new int[a.length + b.length];
int x = 0;
int y = 0;
for (int i = 0; i < a.length; i++) {
for (int j = y; j < b.length; j++, y++) {
if(a[i] <= b[j]) {
c[x++] = a[i];
break;
}
c[x++] = b[j];
}
}
while (y < b.length) {
c[x++] = b[y++];
}
return c;
} public static void printArray(int[] c) {
for (int i = 0; i < c.length; i++) {
System.out.print(c[i] + " ");
}
}
}