Bubble, selection, and insertion sorting The following programs are bubble sorting, selection sorting and insertion sorting. As most of the programs online or in books are with Arrays and not ArrayList, I am unsure if they are correct.



BubbleSort:

    public class BubbleSort {

      List&lt;Integer&gt; unsortedList;

      private BubbleSort() {
        unsortedList = new ArrayList&lt;Integer&gt;();
        unsortedList.add(0, 6);
        unsortedList.add(1, 3);
        unsortedList.add(2, 7);
        unsortedList.add(3, 4);
        unsortedList.add(4, 2);
        unsortedList.add(5, 3);
        unsortedList.add(6, 8);
    }

    private void sortData() {
        ArrayList&lt;Integer&gt; tempList = (ArrayList&lt;Integer&gt;) this.unsortedList;
        int size = unsortedList.size();
        int counter = size;
        do {

            for (int i = 0; i &lt; size-1; i++) {

                if (unsortedList.get(i) &gt; unsortedList.get(i + 1)) {


                    Integer temp1= unsortedList.get(i + 1);
                    Integer temp2= unsortedList.get(i);
                    unsortedList.set(i,temp1);
                    unsortedList.set(i+1,temp2);

                }
            }
            size = size - 1;
        } while (size != 1);
    }

    public static void main(String arg[]) {

        BubbleSort obj = new BubbleSort();
        obj.sortData();

        for(Integer i:obj.unsortedList){
            System.out.println(i);
        }
    }}




SelectionSort:

    public class SelectionSort {

    List&lt;Integer&gt; unsortedList;

    private SelectionSort() {
        unsortedList = new ArrayList&lt;Integer&gt;();
        unsortedList.add(0, 16);
        unsortedList.add(1, 3);
        unsortedList.add(2, 7);
        unsortedList.add(3, 4);
        unsortedList.add(4, 12);
        unsortedList.add(5, 3);
        unsortedList.add(6, 8);
        unsortedList.add(7, 18);
        unsortedList.add(8, 81);
        unsortedList.add(9, 2);
    }

    public void sortData() {

    int totalPass = unsortedList.size();
    int listLength = totalPass;

    for (int i = 0; i &lt;= totalPass - 1; i++) {
        int pointerSmallPosition=i;
        for (int j = i; j &lt; listLength-1; j++) {

            if (unsortedList.get(pointerSmallPosition) &gt; unsortedList.get(j + 1)) {
                pointerSmallPosition=j + 1;
            }
        }

        int temp1= unsortedList.get(i);
        int temp2= unsortedList.get(pointerSmallPosition);
        unsortedList.set(i,temp2);
        unsortedList.set(pointerSmallPosition,temp1); 
    }

}

public static void main(String arg[]) {

    SelectionSort obj = new SelectionSort();
    obj.sortData();

    for (Integer i : obj.unsortedList) {
        System.out.println(i);
    }
}}




InsertionSort:

 public class InsertionSort {

    List&lt;Integer&gt; unsortedList;

    public InsertionSort(){
        unsortedList = new ArrayList&lt;Integer&gt;();
        unsortedList.add(0, 6);
        unsortedList.add(1, 3);
        unsortedList.add(2, 7);
        unsortedList.add(3, 4);
        unsortedList.add(4, 2);
        unsortedList.add(5, 3);
        unsortedList.add(6, 8);
    }

    public static void main(String arg[]){

        InsertionSort obj=new InsertionSort();
        obj.sortData();

        for(Integer i:obj.unsortedList){
            System.out.println(i);
        }

    }

    private void sortData() {

        int size = unsortedList.size();
        int sortedSize=0;
        for(int i=1;i&lt;size-1;i++){
            int j=i;
            do{
                if(unsortedList.get(j)&lt;unsortedList.get(j-1)){
                    int small=unsortedList.get(j);
                    int large=unsortedList.get(j-1);
                    unsortedList.set(j-1, small);
                    unsortedList.set(j, large);
                }
                j--;
            }while(j&gt;sortedSize);

        }}}
They seem right to me. A few minor comments:


You don't need to construct your lists by adding .add(pos, value). You can build them from an array as:

int[] array = {6,12,9,-14};
List&lt;Integer&gt; list = new ArrayList&lt;&gt;(Arrays.asList(array);


which is more concise and readable.
In your bubblesort, you do:

Integer temp1= unsortedList.get(i + 1);
Integer temp2= unsortedList.get(i);


accessing unsortedList twice. If you assign outside the if, you can recycle it and make it more readable. Also, consider using better names than temp1 and temp2: not much difference here, but it's a good habit to get into, same for insertion.