有关顺序表的合并,帮帮忙把!~~~用JAVA编的,谢谢了~~

来源:百度知道 编辑:UC知道 时间:2024/06/06 18:07:33
数据结构(JAVA):有顺序表A和B,其元素均按从小到大的升序排列,编写一个算法将它们合并成一个顺序表C,要求C的元素也是从小到大的升序排列。

//以数组为例,a、b分别为有序数组,将其合并为有序数组c

public class I {
public static void main(String[] args) {
int[] a = { 2, 3, 3, 3, 4, 4, 5, 6};
int[] b = { 1, 2, 3, 3, 3, 4, 5, 5, 6, 8, 9 };
int[] c = new int[a.length + b.length];

int pos = 0;
for (int i = 0, j = 0; i < a.length; i++) {
while(j==b.length){
c[pos]=a[i];
i++;
if(i==a.length){
break;
}
pos++;
}
for (; j < b.length; j++) {

if (a[i] <= b[j]) {
c[pos] = a[i];
pos++;
while(i==a.length-1){
c[pos]=b[j];
j++;
if(j==b.length){
break;
}
pos++;
}
break;
} else {
c[pos] = b[j];
pos++;
i--;
j++;
break;
}
}
}

for (int i = 0; i < c.length; i++){
System.out.print(c[i]+",");
}
}

}

public class Seq
{
public static void main(String[] args){