Can someone give me a better solution for merging two sorted arrays into another sorted array

public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] arrA = {2,3,4};
        int[] arrB = {5,6,7,8};
        mergeArray(arrA,arrB);
    }
    static void mergeArray(int[] arrA,int[] arrB){

        int len = arrA.length+arrB.length;
        int lenA = arrA.length;
        int lenB = arrB.length;
        int a=0,b=0,c=0;
        int temp=0;
        int[] arrC = new int[len];
        for(int i=0; i < lenA; i++){
            arrC[i] = arrA[i];
        }
        for(int j=lenA,k=0; (k < lenB) && (j < len) ; j++,k++){
            arrC[j] = arrB[k];
        }
        for(int n=0; n < len ; n++){
            for(int m=0; m < len; m++ ){
                if(arrC[n] < arrC[m]){
                    temp  = arrC[n];
                    arrC[n] = arrC[m];
                    arrC[m] = temp;
                }
            }
        }
        for(int x : arrC){
            System.out.println(x);
        }
    }

Result: {2,3,4,5,6,7,8}

I am trying to put the values ​​in one new array and sort them again. Can anyone give me a better solution than this.

+4
source share
5 answers

I remembered the old school days! Here is the solution! No libraries, just code! Enjoy it.

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        int [] array1 = {5, 1, 4, 5, 7, 8, 1, 0, 4};
        int [] array2 = {4, 7, 1, 0, 9, 3};

        System.out.println("Array 1");
        print(array1);

        System.out.println("Array 2");
        print(array2);

        Arrays.sort(array1);
        Arrays.sort(array2);

        System.out.println("Sorted array 1");
        print(array1);

        System.out.println("Sorted array 2");
        print(array2);

        int [] mergedAndSortedArray = mergeSorted(array1, array2);

        System.out.println("Sorted merged array");
        print(mergedAndSortedArray);
    }

    private static void print(int [] array) {
        for (int i : array) {
            System.out.print(i + " ");
        }

        System.out.println("\n");
    }

    private static int [] mergeSorted(int [] array1, int [] array2) {
        int [] res = new int [array1.length + array2.length];

        int i = 0;
        int j = 0;
        int k = 0;

        //Do your homework. First loop until you reach end of either array, and then add the rest elements.

        return res;
    }
}

This is the result.

Array 1
5 1 4 5 7 8 1 0 4 

Array 2
4 7 1 0 9 3 

Sorted array 1
0 1 1 4 4 5 5 7 8 

Sorted array 2
0 1 3 4 7 9 

Sorted merged array
0 0 1 1 1 3 4 4 4 5 5 7 7 8 9 

Update

N , ( , ..), , , , , !

+2

. , . O (n ^ 2) . , , , .

, . , , . . O (n), n - .

merge sort. , . , , .

+2

Java8:

    int[] arrA = {2,3,4};
    int[] arrB = {5,6,7,8};
    int[] mergedArray = IntStream.concat(Arrays.stream(arrA), Arrays.stream(arrB)).toArray();

:

IntStream.concat(Arrays.stream(arrA), Arrays.stream(arrB)).sorted().toArray();

, .

+2

:

Integer[] mergeArray = (Integer[])ArrayUtils.addAll(arrA, arrB);

:

public static T [] addAll (T [] array1, T... array2)

:

Arrays.sort(mergeArray, Collections.reverseOrder());
+1

, "" , , . , , , .

, , .

, :

  • leftIndex ( )
  • rightIndex ( )
  • destinationIndex ( )

. .

While these values ​​are less than the next value from another input array, we add it to the output. Then we move on to another array. We keep switching back and forth until we reach the end of both inputs.

pseudo code:

while (leftIndex and rightIndex are both within the length of their arrays) {
    while(leftInput[leftIndex] is less than rightInput[rightIndex]) {
         copy leftInput[leftIndex] into destination[destinationIndex]
         increment leftIndex
         increment destinationIndex
    }
    while(rightInput[rightIndex] is less than leftInput[leftIndex]) {
         copy rightInput[leftIndex] into destination[destinationIndex]
         increment rightIndex
         increment destinationIndex
    }
}

If you translate what I wrote directly in Java, you are likely to get exceptions ArrayIndexOutOfBounds. You need to add additional code to the operator whileso that it does the right thing when one of the indexes is at the end of its array.

+1
source

Source: https://habr.com/ru/post/1679468/


All Articles