combining and sorting two arrays Java?

arrays, java, sorting

Solution

This is how it should be in a simple way:

public static void main(String[] args) {

    int a [] = {3,5,7,9,12,14, 15};
    int b [] = {6 ,7, 10};
    int j = 0, k = 0;

    //output array should be 3,5,6,7,7,9,10,12,14,15

    int c [] = new int[a.length+b.length];//10 values

    // we're filling c with the next appropriate number
    // we start with checking a[0] and b[0] till we add
    // all the elements
    for (int i = 0; i < c.length; i++) {
        // if both "a" and "b" have elements left to check
        if (j < a.length && k < b.length) {
            // check if "b" has a smaller element
            if (b[k] < a[j]) {
                // if so add it to "c"
                c[i] = b[k];
                k++;
            }
            // if "a" has a smaller element
            else {
                // add it to "c"
                c[i] = a[j];
                j++;
            }       
        }
        // if there are no more elements to check in "a"
        // but there are still elements to check in "b"
        else if (k < b.length) {
            // add those elements in "b" to "c"
            c[i] = b[k];
            k++;
        }
        // if there are no more elements to check in "b"
        // but there are still elements to check in "a"
        else {
            // add those elements in "a" to "c"
            c[i] = a[j];
            j++;
        }
    }

    for(int i = 0; i < c.length; i++)
        System.out.println(c[i]);
}

Hope it helps.

Problem

So basically there are two separate presorted arrays, and you have to combine them and sort them (without sort() methods of course). Here is my code: ``` public static void main(String[] args) { int a [] = {3,5,7,9,12,14, 15}; int b [] = {6 ,7, 10}; int j = 0; //output array should be 3,5,6,7,7,9,10,12,14,15 int c [] = new int[a.length+b.length];//10 values for (int i = 0;i<b.length;i++){ while(b[i]>a[j]){ c[j] = a[j] ; j++; } if(b[i] == a[j]){ c[j] = b[i]; c[j+1] = a[j]; } c[j] = b[i]; j++; } for(int i = 0;i<c.length;i++) System.out.println(c[i]); } ``` I'm guessing the zeros I am getting are from a mistake in one of the booleans (< & >), but I cant seem to figure it out. It works fine for the first 4, but once I get to the repeating 7's, it just goes crazy. Please help me understand, don't just change the code.

Original source

Related problems