pointlesseducation
pointlesseducation

Reputation: 13

How to merge two integer arrays in ascending order?

How can I merge two arrays in ascending order? I have the code below and I need to merge this array in ascending order, and it should be in a separate method. So in this case the method should return {1, 4, 8, 9, 11, 12, 13}

public class MergingArrays {
    public static void main(String[] args) {
        int [] array1 = {4, 8, 12, 13};
        int [] array2 = {1, 9, 11};
        merge(array1, array2);
    } 
    public static int[] merge(int array1[], int array2[]) {

    }
}

Upvotes: 1

Views: 2252

Answers (4)

Prilaga
Prilaga

Reputation: 857

I had similar task on interview. The main logic is to compare each element in both arrays until one ends. And then just append the rest of the numbers.

static int[] geMergedArrayC(int[] arrayA, int[] arrayB) {

        // Count the length
        int lengthA = arrayA.length;
        int lengthB = arrayB.length;
        int lengthC = lengthA + lengthB;
        int[] arrayC = new int[lengthC];
        
        // Cursors to keep the position
        int cursorA = 0;
        int cursorB = 0;
        int cursorC = 0;
        
        // Store the temp values
        int aValue;
        int bValue;

        for (; cursorC < lengthC; cursorC++) {
            // Add from both arrays until one ends
            if (cursorA < lengthA && cursorB < lengthB) {
                aValue = arrayA[cursorA];
                bValue = arrayB[cursorB];
                if (aValue <= bValue) {
                    // Add from array A
                    arrayC[cursorC] = aValue;
                    cursorA++;
                } else {
                    // Add from array B
                    arrayC[cursorC] = bValue;
                    cursorB++;
                }

            } else if (cursorA < lengthA) {
                // Add from array A
                aValue = arrayA[cursorA];
                arrayC[cursorC] = aValue;
                cursorA++;

            } else {
                // Add from array B
                bValue = arrayB[cursorB];
                arrayC[cursorC] = bValue;
                cursorB++;
            }
        }

        return arrayC;
    }

Upvotes: 0

Deep
Deep

Reputation: 152

You can use apache commons class and the sort function of Collections class

import org.apache.commons.lang.ArrayUtils;
import java.util.Collections;

public class MergingArrays {
    public static void main(String[] args) {
        int [] array1 = {4, 8, 12, 13};
        int [] array2 = {1, 9, 11};
        merge(array1, array2);
    } 
    public static int[] merge(int array1[], int array2[]) {
    return Collections.sort(ArrayUtils.addAll(array1, array2));
    }
}

Upvotes: 0

user16123931
user16123931

Reputation:

public static int[] merge(int[] array1, int[] array2) {
        int totalElements = array1.length + array2.length, array2Index = 0;
        int[] toReturn = new int[totalElements];

        for (int i = 0; i < array1.length; i++) {
            toReturn[i] = array1[i];
        }
        for (int i = array1.length; i < totalElements; i++) {
            toReturn[i] = array2[array2Index++];
        }

        //Manual Ascending Array Sorting
        for (int i = 0; i < totalElements; i++) {
            for (int j = 0; j < totalElements; j++) {
                if(toReturn[i] < toReturn[j]) {
                    int temp = toReturn[i];
                    toReturn[i] = toReturn[j];
                    toReturn[j] = temp;
                }
            }
        }
        return toReturn;
    }

Upvotes: 0

user12392751
user12392751

Reputation:

Assuming that you are looking to merge two sorted arrays, you could do it like this.

public static int[] merge(int array1[], int array2[]) {
    int i = 0, j = 0, k = 0;
    int size1 = array1.length;
    int size2 = array2.length;
    int[] result = new int[size1 + size2];

    while (i < size1 && j < size2) {
        if (array1[i] < array2[j]) {
            result[k++] = array1[i++];
        } else {
            result[k++] = array2[j++];
        }
    }

    // Store remaining elements
    while (i < size1) {
        result[k++] = array1[i++];
    }

    while (j < size2) {
        result[k++] = array2[j++];
    }
    
    return result;

}

Upvotes: 1

Related Questions