java_logo

Java merge and sort lists of integers without using pre-defined functions

In the previous blog post http://www.jijichiz.com/java-merge-sort-integer-arrays-without-using-pre-defined-functions/, we helped George define a function to merge and sort lists of integers. But in this next post, we are now going to help him merge and sort lists of integers. Same problem, he’s got two unordered lists of integers such as below:

[2, 3, 1]

[2, 5, 5, 8, 9, 13]

Now, he wants to merge these two and sort them in ascending order. He is not allowed to use any sort functions to solve this problem. The result should display:

[1, 2, 2, 3, 5, 5, 8, 9, 13]

To solve this problem, let’s break down our solution into smaller steps. This time, for Java list.

First step is to merge:

static List mergeList(List list1, List list2) {
        List mergedList = new ArrayList(list1);
        mergedList.addAll(list2);
        return mergedList;
}

Second step is to sort:

And then to sort it, we will implement Bubble sort algorithm. (Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. source: Wikipedia)

static List sortList(List mergedList) {
        for(int i = 0; i < mergedList.size(); i++) {
            for(int j = i + 1; j < mergedList.size(); j++) {
                int tmp = 0;
                if(mergedList.get(i) > mergedList.get(j)) {
                    tmp = mergedList.get(i);
                    mergedList.set(i, mergedList.get(j));
                    mergedList.set(j, tmp);
                }
            }
        }
        return mergedList;
}

This is to sort in ascending order. If you want to sort in descending order, just change the operand from > (greater than) to < (less than).

static List sortList(List mergedList) {
        for(int i = 0; i < mergedList.size(); i++) {
            for(int j = i + 1; j < mergedList.size(); j++) {
                int tmp = 0;
                if(mergedList.get(i) < mergedList.get(j)) {
                    tmp = mergedList.get(i);
                    mergedList.set(i, mergedList.get(j));
                    mergedList.set(j, tmp);
                }
            }
        }
        return mergedList;
}

And that's it! If you want the entire code, please refer to solution below:

import java.util.List;
import java.util.ArrayList;
public class MyClass {
    public static void main(String args[]) {
        List list1 = new ArrayList();
        list1.add(2);
        list1.add(3);
        list1.add(1);
        
        List list2 = new ArrayList();
        list2.add(2);
        list2.add(5);
        list2.add(5);
        list2.add(8);
        list2.add(9);
        list2.add(13);
        
        System.out.println(sortList(mergeList(list1, list2)));
    }
    static List mergeList(List list1, List list2) {
        List mergedList = new ArrayList(list1);
        mergedList.addAll(list2);
        return mergedList;
    }
    static List sortList(List mergedList) {
        for(int i = 0; i < mergedList.size(); i++) {
            for(int j = i + 1; j < mergedList.size(); j++) {
                int tmp = 0;
                if(mergedList.get(i) > mergedList.get(j)) {
                    tmp = mergedList.get(i);
                    mergedList.set(i, mergedList.get(j));
                    mergedList.set(j, tmp);
                }
            }
        }
        return mergedList;
    }
}

Result:

[1, 2, 2, 3, 5, 5, 8, 9, 13]

I recommend using http://www.jdoodle.com to test it online if you don't have any installed IDE on your local machines. If you have any questions, feel free to drop your comments 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.