Bubble Sort

public class BubbleSort {
    public static void sort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    // swap arr[j] and arr[j+1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
                printArray(arr);
            }
        }
    }

    private static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
    
    // Tester method
    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};
        System.out.println("Original array:");
        printArray(arr);
        System.out.println("\nSorting steps:");
        sort(arr);
        System.out.println("\nSorted array:");
        printArray(arr);
    }
}

BubbleSort.main(null);
Original array:
64 34 25 12 22 11 90 

Sorting steps:
34 64 25 12 22 11 90 
34 25 64 12 22 11 90 
34 25 12 64 22 11 90 
34 25 12 22 64 11 90 
34 25 12 22 11 64 90 
34 25 12 22 11 64 90 
25 34 12 22 11 64 90 
25 12 34 22 11 64 90 
25 12 22 34 11 64 90 
25 12 22 11 34 64 90 
25 12 22 11 34 64 90 
12 25 22 11 34 64 90 
12 22 25 11 34 64 90 
12 22 11 25 34 64 90 
12 22 11 25 34 64 90 
12 22 11 25 34 64 90 
12 11 22 25 34 64 90 
12 11 22 25 34 64 90 
11 12 22 25 34 64 90 
11 12 22 25 34 64 90 
11 12 22 25 34 64 90 

Sorted array:
11 12 22 25 34 64 90 

Selection Sort

public class SelectionSort {
    public static void sort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            // swap arr[i] and arr[minIndex]
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
            printArray(arr);
        }
    }

    private static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
    
    // Tester method
    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};
        System.out.println("Original array:");
        printArray(arr);
        System.out.println("\nSorting steps:");
        sort(arr);
        System.out.println("\nSorted array:");
        printArray(arr);
    }
}

SelectionSort.main(null);
Original array:
64 34 25 12 22 11 90 

Sorting steps:
11 34 25 12 22 64 90 
11 12 25 34 22 64 90 
11 12 22 34 25 64 90 
11 12 22 25 34 64 90 
11 12 22 25 34 64 90 
11 12 22 25 34 64 90 

Sorted array:
11 12 22 25 34 64 90 

Insertion Sort

public class InsertionSort {
    public static void sort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; ++i) {
            int key = arr[i];
            int j = i - 1;

            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
            printArray(arr);
        }
    }

    public static void printArray(int[] arr) {
        for (int i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {12, 11, 13, 5, 6};
        System.out.println("Original array:");
        printArray(arr);
        System.out.println("\nSorting steps:");
        sort(arr);
        System.out.println("\nSorted array:");
        printArray(arr);
    }
}

InsertionSort.main(null);
Original array:
12 11 13 5 6 

Sorting steps:
11 12 13 5 6 
11 12 13 5 6 
5 11 12 13 6 
5 6 11 12 13 

Sorted array:
5 6 11 12 13 

Merge Sort

public class MergeSort {
    public static void sort(int[] arr, int l, int r) {
        if (l < r) {
            int m = (l + r) / 2;

            sort(arr, l, m);
            sort(arr, m + 1, r);

            merge(arr, l, m, r);
            printArray(arr);
        }
    }

    public static void merge(int[] arr, int l, int m, int r) {
        int n1 = m - l + 1;
        int n2 = r - m;

        int[] L = new int[n1];
        int[] R = new int[n2];

        for (int i = 0; i < n1; ++i) {
            L[i] = arr[l + i];
        }
        for (int j = 0; j < n2; ++j) {
            R[j] = arr[m + 1 + j];
        }

        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            }
            else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    public static void printArray(int[] arr) {
        for (int i : arr) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = {12, 11, 13, 5, 6, 7};
        System.out.println("Original array:");
        printArray(arr);
        System.out.println("\nSorting steps:");
        int n = arr.length;
        sort(arr, 0, n - 1);
        System.out.println("\nSorted array:");
        printArray(arr);
    }
}

MergeSort.main(null);
Original array:
12 11 13 5 6 7 

Sorting steps:
11 12 13 5 6 7 
11 12 13 5 6 7 
11 12 13 5 6 7 
11 12 13 5 6 7 
5 6 7 11 12 13 

Sorted array:
5 6 7 11 12 13