lol
lol

Reputation: 25

merging two sorted arraylist into one sorted arraylist

Here is my code:

 for (int i = 0, j = 0; i < array1.size() && j < array2.size();) {
    if (array1.get(i) < array2.get(j)) {
        list.add(array1.get(i));
        i++;
    } else {
        list.add(array2.get(j));
        j++;
    }
}

The problem is that I can't get to the largest number. For example, if I have two arrays {1, 3, 5} and {2, 4, 6}, that code gives me {1, 2, 3, 4, 5}. How can I fix it?

Upvotes: 0

Views: 2263

Answers (4)

gati sahu
gati sahu

Reputation: 2626

In above your code your using condition i < array1.size() && j < array2.size() so if one list exhaust first then it will go out of loop and will not process the remaining element in the second list.So after the loop you need to process the list if the counter is not reached the end.

 int i = 0;
            int j = 0;
            while (i < array1.size() && j < array2.size()) {
                if (array1.get(i) < array2.get(j)) {
                    list.add(array1.get(i));
                    i++;
                } else {
                    list.add(array2.get(j));
                    j++;
                }
            }
            while (i < array1.size()) {
                list.add(array1.get(i));
                i++;
            }
            while (j < array2.size()) {
                list.add(array2.get(j));
                j++;
            }

Upvotes: 0

Ashraful Islam
Ashraful Islam

Reputation: 12830

Add Integer Max Value at the end of both of the list and change the and condition to or condition

List<Integer> array1 = new ArrayList<>(Arrays.asList(1, 3, 5));
List<Integer> array2 = new ArrayList<>(Arrays.asList(2, 4, 6));

List<Integer> list = new ArrayList<>();

array1.add(Integer.MAX_VALUE); //Add Integer Max Value
array2.add(Integer.MAX_VALUE); //Add Integer Max Value

for (int i = 0, j = 0; i < array1.size() - 1 || j < array2.size() - 1; ) {
    if (array1.get(i) < array2.get(j)) {
        list.add(array1.get(i));
        i++;
    } else {
        list.add(array2.get(j));
        j++;
    }
}

System.out.println(list);

array1.remove(array1.size() - 1); //Remove Integer Max Value
array2.remove(array2.size() - 1); //Remove Integer Max Value

Upvotes: 0

NiVeR
NiVeR

Reputation: 9786

The answers using stream are correct, but I would like to provide the solution starting from your code for completeness.

int i, j;
for (i = 0, j = 0; i < array1.size() && j < array2.size();) {
    if (array1.get(i) < array2.get(j)) {
        list.add(array1.get(i));
        i++;
    } else {
        list.add(array2.get(j));
        j++;
    }
}

while(i < array1.size()) list.add(array1.get(i++));
while(j < array2.size()) list.add(array2.get(j++));

Upvotes: 1

Darshan Mehta
Darshan Mehta

Reputation: 30809

Your for loop will exit if one of i < array1.size() or j < array2.size() conditions fails, meaning it won't process (and sort) remaining elements in the other array.

Here's the stream approach to concatenate and sort two arrays:

int[] a1 = new int[]{1,3,5};
int[] a2 = new int[]{2,4,6};
List<Integer> result = IntStream.concat(Arrays.stream(a1), Arrays.stream(a2))
    .boxed()
    .sorted()
    .collect(Collectors.toList());

System.out.println(result);

Upvotes: 0

Related Questions