Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Merging two sorted arrays

import java.util.Arrays;

public class MergingTwoArrays {
	/*
	 * This code merges two arrays, sorted in ascending
	 * order, into a single aggregate array sorted in
	 * ascending order.
	 * 
	 * Let n be size of first array and
	 * m the size of the second array.
	 * 
	 * Time complexity: O(n+m)
	 * Space complexity: O(n+m)
	 */
	public static void main(String[] args) {
		int[] nums1 = { 1, 5, 7 };
		int[] nums2 = { 3, 6 };
		// Below prints: [1, 3, 5, 6, 7]
		System.out.println(Arrays.toString(merge(nums1, nums2)));
	}

	// Method merging the two sorted arrays
	private static int[] merge(int[] nums1, int[] nums2) {
		int n = nums1.length, m = nums2.length;
		int[] sortedArray = new int[n + m];
		// Indexes into nums1, nums2, and sortedArray
		int nums1Ptr = 0, nums2Ptr = 0, sortedArrayIdx = 0;
		// Collect the elements of nums1 and nums2 in order
		while (nums1Ptr < n && nums2Ptr < m) {
			// Transfer smaller element to sortedArray
			if (nums1[nums1Ptr] < nums2[nums2Ptr]) {
				sortedArray[sortedArrayIdx++] = nums1[nums1Ptr++];
			} else {
				sortedArray[sortedArrayIdx++] = nums2[nums2Ptr++];
			}
		}
		// Transfer elements remaining in either nums1 or nums2
		// to the resulting sortedArray
		while (nums1Ptr < n) {
			sortedArray[sortedArrayIdx++] = nums1[nums1Ptr++];
		}
		while (nums2Ptr < m) {
			sortedArray[sortedArrayIdx++] = nums2[nums2Ptr++];
		}

		return sortedArray;
	}
}
Comment

In-place merge two sorted arrays

def merge(X, Y):
 
    m = len(X)
    n = len(Y)
 
    # Consider each element `X[i]` of list `X[]` and ignore the element if it is
    # already in the correct order; otherwise, swap it with the next smaller
    # element, which happens to be the first element of `Y[]`.
    for i in range(m):
 
        # compare the current element of `X[]` with the first element of `Y[]`
        if X[i] > Y[0]:
 
            # swap `X[i] with `Y[0]`
            temp = X[i]
            X[i] = Y[0]
            Y[0] = temp
 
            first = Y[0]
 
            # move `Y[0]` to its correct position to maintain the sorted
            # order of `Y[]`. Note: `Y[1…n-1]` is already sorted
            k = 1
            while k < n and Y[k] < first:
                Y[k - 1] = Y[k]
                k = k + 1
 
            Y[k - 1] = first
Comment

two sorted arrays merge

public static int[] merge(int[] a, int[] b) {

    int[] answer = new int[a.length + b.length];
    int i = 0, j = 0, k = 0;
    while (i < a.length && j < b.length)
    {
        if (a[i] < b[j])
        {
            answer[k] = a[i];
            i++;
        }
        else
        {
            answer[k] = b[j];
            j++;
        }
        k++;
    }

    while (i < a.length)
    {
        answer[k] = a[i];
        i++;
        k++;
    }

    while (j < b.length)
    {
        answer[k] = b[j];
        j++;
        k++;
    }

    return answer;
}
Comment

how to merge two sorted arrays

// Merge sorted arrays using javascript

let arrrone = [0,3,4,31];
let arrayTwo = [4,6,30,31,33];
let j = 0;
let k = 0;
let mergedArray = [];

let length = arrrone.length + arrayTwo.length;

console.log(length)

for(let i = 0; i<= length - 1; i++){
  if(arrrone[j] < arrayTwo[k]){
    mergedArray.push(arrrone[j]);
    j++;
  }
  else if(arrrone[j] > arrayTwo[k]){
    mergedArray.push(arrayTwo[k]);
    k++;
  }
  else if(arrrone[j] == arrayTwo[k]){
    mergedArray.push(arrrone[j]);
    mergedArray.push(arrayTwo[k]);
    j++;
    k++;
  }
}

console.log(mergedArray)
Comment

Merge two sorted arrays

def mergeArrays(arr1, arr2, n1, n2):
    arr3 = [None] * (n1 + n2)
    i = 0
    j = 0
    k = 0
 
    while i < n1 and j < n2:
        if arr1[i] < arr2[j]:
            arr3[k] = arr1[i]
            k = k + 1
            i = i + 1
        else:
            arr3[k] = arr2[j]
            k = k + 1
            j = j + 1
 
    while i < n1:
        arr3[k] = arr1[i]
        k = k + 1
        i = i + 1
 
    while j < n2:
        arr3[k] = arr2[j]
        k = k + 1
        j = j + 1
Comment

PREVIOUS NEXT
Code Example
Java :: How to implement the A* shortest path algorithm, in Java? 
Java :: how to get orientation lock to portrait android stackoverflow 
Java :: jframe maximized at startup 
Java :: initialize mocks 
Java :: Implementing the ArrayList Class in java list 
Java :: array reverse in java 
Java :: Create JDBC connection using properties file 
Java :: minecraft detect specific item in chest with custom name 
Java :: finding length of arrays in java 
Java :: how to delete an element from an array in java data structure 
Java :: leer XML java 
Java :: how to spilt the string with comma in jaav 
Java :: find the unique element in a list java 
Java :: read many lines from stdn java 
Java :: java stream code to ignore null 
Java :: object type in java 
Java :: java list remove 
Java :: set union java 
Java :: Sample HashMap 
Java :: android studio clear views of layout 
Java :: redshift establish connection jav gradle 
Java :: Printing cube root of a number in java 
Java :: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction java.lang.reflect.InvocationTargetException (no error message) 
Java :: Java if...else 
Java :: map.keyset in java 
Java :: java stream add to existing list 
Java :: java map key set 
Java :: Java Creating an EnumMap 
Java :: convert string to boolean java 
Java :: encapsulation java 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =