两个有序数组合并为一个有序数组
2017-10-13 10:43
176 查看
先依次比较两个数组,按照小的就传入新的数组。当这次比较完之后可能有一个数组的长度很长,留下一些数组,然后在新数组的末尾插入即可。
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 class ArraySort
{
//两个有序数组的合并函数
public static int[] MergeList(int a[],int b[])
{
int result[];
if(checkSort(a) && checkSort(b)) //检查传入的数组是否是有序的
{
result = new int[a.length+b.length];
int i=0,j=0,k=0; //i:用于标示a数组 j:用来标示b数组 k:用来标示传入的数组
while(i<a.length && j<b.length)
if(a[i] <= b[j]) {
result[k++] = a[i++];
}else{
result[k++] = b[j++];
}
/* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
while(i < a.length)
result[k++] = a[i++];
while(j < b.length)
result[k++] = b[j++];
return result;
}
else
{
System.out.print("非有序数组,不可排序!");
return null;
}
}
//检查数组是否是顺序存储的
public static boolean checkSort(int a[])
{
boolean change = true; //这个标志位是一种优化程序的方法,可以看看我写的冒泡排序优化就会明白了
for(int i=0; i<a.length-1 && change; i++)
{
for(int j=i+1; j<a.length; j++)
if(a[j-1] > a[j])
return false;
else change = false;
}
return true;
}
// 打印函数
public static void print(int b[])
{
for(int i=0; i<b.length ; i++)
{
System.out.print(b[i] + (i%10 ==9 ? "\n":"\t"));
}
}
public static void main(String args[])
{
int a[]={1,2,2,3,5,6,7,7};
int b[]={1,2,4,5,8,8,9,10,11,12,12,13,14};
int c[]= MergeList(a,b);
if(c!=null)
print(c);
else
System.out.println("");
}
}
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 class ArraySort
{
//两个有序数组的合并函数
public static int[] MergeList(int a[],int b[])
{
int result[];
if(checkSort(a) && checkSort(b)) //检查传入的数组是否是有序的
{
result = new int[a.length+b.length];
int i=0,j=0,k=0; //i:用于标示a数组 j:用来标示b数组 k:用来标示传入的数组
while(i<a.length && j<b.length)
if(a[i] <= b[j]) {
result[k++] = a[i++];
}else{
result[k++] = b[j++];
}
/* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
while(i < a.length)
result[k++] = a[i++];
while(j < b.length)
result[k++] = b[j++];
return result;
}
else
{
System.out.print("非有序数组,不可排序!");
return null;
}
}
//检查数组是否是顺序存储的
public static boolean checkSort(int a[])
{
boolean change = true; //这个标志位是一种优化程序的方法,可以看看我写的冒泡排序优化就会明白了
for(int i=0; i<a.length-1 && change; i++)
{
for(int j=i+1; j<a.length; j++)
if(a[j-1] > a[j])
return false;
else change = false;
}
return true;
}
// 打印函数
public static void print(int b[])
{
for(int i=0; i<b.length ; i++)
{
System.out.print(b[i] + (i%10 ==9 ? "\n":"\t"));
}
}
public static void main(String args[])
{
int a[]={1,2,2,3,5,6,7,7};
int b[]={1,2,4,5,8,8,9,10,11,12,12,13,14};
int c[]= MergeList(a,b);
if(c!=null)
print(c);
else
System.out.println("");
}
}
相关文章推荐
- 两个有序数组合并为一个有序数组
- 如何将两个有序的一维数组合并为一个有序的一维数组合
- 合并两个有序数组为一个有序数组
- 两个有序数组合并成一个有序数组
- 两个有序数组合并成一个大的有序数组
- 两个有序数组合并为一个及归并排序实例
- 两个有序数组合并为一个有序数组
- 两个有序数组合并成一个有序数组
- java实现把两个有序数组合并到一个数组
- leetcode:Merge Sorted Array(合并两个有序数组到其中一个数组中)【面试算法题】
- 将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。
- 两个有序数组合并成一个有序数组
- 面试题:两个无序数组合并成一个有序数组
- 算法 - 合并两个有序数组为一个有序数组
- 两个有序数组合并成一个有序数组
- 面试or笔试4——合并两个有序数组为一个有序数组
- 将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。
- 两个有序的数组,合并成一个有序的数组
- Interview Q&A - 合并两个有序数组为一个有序数组(C++)
- 两个有序数组合并为一个有序数组