merge Two Sorted Arrays

package com.company;

public class Algorithm {
    int[] mergeArray(int[] array1, int[] array2) {
        if (0 == array1.length) {
            System.out.println("array1 is empty");
            return array2;
        }
        if (0 == array2.length) {
            System.out.println("array2 is empty");
            return array1;
        }
        int[] array3 = new int[array1.length + array2.length];
        int arr1indx = 0, arr2indx = 0, arr3indx = 0;
        while (arr1indx < array1.length && arr2indx < array2.length) {
            if (array1[arr1indx] < array2[arr2indx]) {
                array3[arr3indx] = array1[arr1indx];
                arr1indx++;
                arr3indx++;
            } else {
                array3[arr3indx] = array2[arr2indx];
                arr2indx++;
                arr3indx++;
            }
        }
        while (arr1indx < array1.length) {
            array3[arr3indx] = array1[arr1indx];
            arr1indx++;
            arr3indx++;
        }
        while (arr2indx < array2.length) {
            array3[arr3indx] = array2[arr2indx];
            arr2indx++;
            arr3indx++;
        }
        return array3;
    }
}
package com.company;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AlgorithmTest {
    @Test
    void mergeArray1() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{};
        int[] array2 = new int[]{2, 4, 6};
        int[] array3 = algorithm.mergeArray(array1, array2);
        Assertions.assertArrayEquals(new int[]{2, 4, 6}, array3);
    }

    @Test
    void mergeArray2() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5};
        int[] array2 = new int[]{};
        int[] array3 = algorithm.mergeArray(array1, array2);
        Assertions.assertArrayEquals(new int[]{1, 3, 5}, array3);
    }

    @Test
    void mergeArray3() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5};
        int[] array2 = new int[]{2, 4, 6};
        int[] array3 = algorithm.mergeArray(array1, array2);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6}, array3);
    }

    @Test
    void mergeArray4() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5};
        int[] array2 = new int[]{2, 4, 6, 8};
        int[] array3 = algorithm.mergeArray(array1, array2);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 8}, array3);
    }

    @Test
    void mergeArray5() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5, 7};
        int[] array2 = new int[]{2, 4, 6};
        int[] array3 = algorithm.mergeArray(array1, array2);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 7}, array3);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

package com.company;

public class Algorithm {
    int[] mergeArray(int[] array1, int array1Size, int[] array2, int array2Size) {
        if (array1.length != array1Size + array2Size) {
            throw new RuntimeException("Invalid length");
        }
        if (0 == array1.length) {
            System.out.println("array1 is empty");
            return array2;
        }
        if (0 == array2.length) {
            System.out.println("array2 is empty");
            return array1;
        }
        int arr1indx = array1Size - 1, arr2indx = array2Size - 1, arr3indx = array1Size + array2Size - 1;
        while (arr1indx >= 0 && arr2indx >= 0) {
            if (array1[arr1indx] > array2[arr2indx]) {
                array1[arr3indx] = array1[arr1indx];
                arr1indx--;
                arr3indx--;
            } else {
                array1[arr3indx] = array2[arr2indx];
                arr2indx--;
                arr3indx--;
            }
        }
        while (arr2indx >= 0) {
            array1[arr3indx] = array2[arr2indx];
            arr2indx--;
            arr3indx--;
        }
        return array1;
    }
}
package com.company;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class AlgorithmTest {
    @Test
    void mergeArray1() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{};
        int[] array2 = new int[]{2, 4, 6};
        assertThrows(RuntimeException.class, () -> algorithm.mergeArray(array1, 3, array2, 0));
    }

    @Test
    void mergeArray2() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5};
        int[] array2 = new int[]{};
        int[] array3 = algorithm.mergeArray(array1, 3, array2, 0);
        Assertions.assertArrayEquals(new int[]{1, 3, 5}, array3);
    }

    @Test
    void mergeArray3() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5, 0, 0, 0};
        int[] array2 = new int[]{2, 4, 6};
        int[] array3 = algorithm.mergeArray(array1, 3, array2, 3);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6}, array3);
    }

    @Test
    void mergeArray4() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5, 0, 0, 0, 0};
        int[] array2 = new int[]{2, 4, 6, 8};
        int[] array3 = algorithm.mergeArray(array1, 3, array2, 4);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 8}, array3);
    }

    @Test
    void mergeArray5() {
        Algorithm algorithm = new Algorithm();
        int[] array1 = new int[]{1, 3, 5, 7, 0, 0, 0};
        int[] array2 = new int[]{2, 4, 6};
        int[] array3 = algorithm.mergeArray(array1, 4, array2, 3);
        Assertions.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 7}, array3);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

follow us on