算法研究之合并两个已排序的数组java版

package com.zken.test;

/**
 * @author iamzken
 * 2015-8-28
 * 合并两个有序数组
 *
 */
public class Sorter2 {

public static void merge2SortedArray(int[] a , int[] b , int[] c){
//a数组的当前索引
int i = 0;
//b数组的当前索引
int j = 0;
//c数组的当前索引
int k = 0;
//循环,只要a和b都没有遍历完就一直循环
while(i < a.length && j < b.length){
//如果当前a[i]比b[j]小,就把c[k]元素置为a[i],同时k++,i++
if(a[i] < b[j]){
c[k++] = a[i++];
//否则,如果当前a[i]比b[j]大,就把c[k]元素置为b[j],同时k++,j++
}else{
c[k++] = b[j++];
}
}

//上个循环能够结束,说明a已经循环完或b已经循环完
//下述两个循环只能有一个满足循环条件
//只要a没有循环完,就把a中剩下的元素依次放入c中
while(i < a.length){
c[k++] = a[i++];
}
//只要b没有循环完,就把b中剩下的元素依次放入c中
while(j < b.length){
c[k++] = b[j++];
}
}
//测试程序
public static void main(String[] args) {
//待合并数组a
int[] a = new int[]{1,3,5,7,9};
//待合并数组b
int[] b = new int[]{2,4,6,8};
//c用来存放合并之后的数组
int[] c = new int[a.length+b.length];
merge2SortedArray(a, b, c);
for(int i = 0;i < c.length;i++){
System.out.print(c[i]+"\t");
}
}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 03:02:28

算法研究之合并两个已排序的数组java版的相关文章

88. 合并两个已排序的数组 Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initi

剑指Offer15 合并两个已排序链表

1 /************************************************************************* 2 > File Name: 15_MergeTwoSortList.cpp 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年08月30日 星期二 15时49分47秒 6 ********************************

数据结构-合并两个已经排序的数组

合并两个已经排序的数组 1 /// <summary> 2 /// 合并两个已经排序的数组 3 /// </summary> 4 /// <param name="arr1"></param> 5 /// <param name="arr2"></param> 6 public static void MergeSortedArray(int[] arr1,int[] arr2) 7 { 8 L

基础典型算法研究:合并有序数组

做leetcode第二题的时候,发现合并有序数组是一个很有意思的问题,于是,总结如下,部分内容来源于网络各位大神. 第一种方法: 合并调用sort. 即是将两个数组合并在一个数组里面,然后对合并后的数组调用sort函数即可. class Solution: def getArray(self, A, B) : for item in B : A.append(item) A.sort() 第二种方法: 极值插入法. #include <stdio.h> void insert(int *arr

【C练习】两个已经从小到大的数组合并成为一个从小到大排序的数组

两个已经从小到大的数组合并成为一个从小到大排序的数组 1 #include<stdio.h> 2 int main() 3 { 4 int m,n,i,j,k,tem=0; 5 printf("这两个数组分别有多少个数:\n"); 6 scanf("%d%d",&m,&n); 7 int a[m],b[n],c[m+n]; 8 printf("从小到大输入%d个数:\n",m); 9 for(i=0;i<m;i+

leetcode 题解:Merge Sorted Array(两个已排序数组归并)

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

合并两个已序单链表

// 合并两个已序单链表 PSListNode MergeList(PSListNode pL1, PSListNode pL2) { PSListNode pNewHead = NULL ; PSListNode pL1Node = pL1 ; PSListNode pL2N0de = pL2 ; PSListNode pNode = NULL ; PSListNode pTailNode = NULL ; if (pL1 == NULL) { return pL2 ; } if (pL2 =

leetcode算法题2: 合并两个二叉树。递归,如何切入并保持清醒?

/* Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, t

算法总结之 合并两个有序的单链表

给定两个有序单链表的头节点head1 和 head2 ,请合并两个有序链表,合并后的链表依然有序,并返回合并后链表的头节点 假设两个链表长度为M和N 直接给出时间复杂度为(M+N) 额外空间复杂度O(1) 1 如果两个链表中一个为空  则无需合并 返回另一个的链表头节点 2 比较head1 和 head2 的值,小的是合并链表的头节点,记为head   在之后的步骤里 哪个链表的头节点值更小,另一个链表的所有节点都会一次插入到这个链表中 3不妨设head节点所在的链表1, 另一个链表2,1和2都