combining and sorting two arrays Java?

21,533

Solution 1

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.

Solution 2

You can try this code.

public static void main(String[] args) {
    int a[] = { 3, 5, 7, 9, 12, 14, 15 };
    int b[] = { 6, 7, 10 };

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

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

    int s[] = null;
    int[] l = null;

    if (alen < blen) {
        s = a;
        l = b;
    } else {
        s = b;
        l = a;
    }
            // Constructing Combined Array
    for (int i = 0, p = 0; i < c.length; i++, p++) {
        if (i == s.length) {
            p = 0;
        }
        if (i < s.length) {
            c[i] = s[p];
        } else {
            c[i] = l[p];
        }
    }
            //Sorting the C array 
    for (int i = 1; i < c.length; i++) {
        int j = i;
        int B = c[i];
        while ((j > 0) && (c[j - 1] > B)) {
            c[j] = c[j - 1];
            j--;
        }
        c[j] = B;
    }

    for (int i = 0; i < c.length; i++)
        System.out.print(c[i]);
}
Share:
21,533
user1229164
Author by

user1229164

Updated on July 20, 2020

Comments

  • user1229164
    user1229164 almost 4 years

    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.

  • peacemaker
    peacemaker almost 12 years
    Simple links should be comments, not answers
  • pb2q
    pb2q almost 12 years
    please provide some substantive content in your answers, not just links to related content
  • ArtemStorozhuk
    ArtemStorozhuk almost 12 years
    @peacemaker copied and pasted algorithm from the article. Is it OK now? :)
  • Leigh
    Leigh almost 12 years
    Did you see their comment: Please help me understand, don't just change the code ? Can you elaborate on what is wrong with their current code?
  • ilalex
    ilalex almost 12 years
    why are you need int s[] = null; int[] l = null;?
  • K_Anas
    K_Anas almost 12 years
    the problem is that he is pointing the same variable j for the cell index of tab a and tab c
  • Akhi
    Akhi almost 12 years
    s[] is for storing the smaller array and l[] is for the larger array.if we know which of the 2 is smaller we can construct c(combined) easily.