import java.util.Scanner;

/**
 * Created by zhangrunfeng on 2019-04-30
 */
public class Main {

    private int arrayLen;
    private int arr[];

    private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    }

    private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    }

    private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    }

    public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    }
}
 import java.util.Scanner; Scanner java.util public class Main {

    private int arrayLen;
    private int arr[];

    private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    }

    private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    }

    private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    }

    public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    }
} Main private int arrayLen; arrayLen private int arr[]; arr private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    } init {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    } arrayLen = sc.nextInt() arrayLen sc.nextInt() sc.nextInt sc arr = new int[arrayLen] arr new int[arrayLen] arrayLen for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        } int idx = 0; int idx = 0; idx 0 idx < arrayLen idx arrayLen idx++ idx++ idx {
            arr[idx] = sc.nextInt();
        } {
            arr[idx] = sc.nextInt();
        } arr[idx] = sc.nextInt() arr[idx] arr idx sc.nextInt() sc.nextInt sc Scanner sc sc private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    } printArray {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    } for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        } int idx = 0; int idx = 0; idx 0 idx < arrayLen-1 idx arrayLen-1 arrayLen 1 idx++ idx++ idx {
            System.out.print(arr[idx] + " ");
        } {
            System.out.print(arr[idx] + " ");
        } System.out.print(arr[idx] + " ") System.out.print System.out System System.out arr[idx] + " " arr[idx] arr idx " " System.out.println(arr[arrayLen-1]) System.out.println System.out System System.out arr[arrayLen-1] arr arrayLen-1 arrayLen 1 private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    } bubbleSort {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    } int count = 0; count 0 boolean flag = true; flag true for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        } int iter = 0; int iter = 0; iter 0 iter < arrayLen-1 iter arrayLen-1 arrayLen 1 iter+=1 iter+=1 iter 1 {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        } {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        } if (!flag) break; !flag flag break; flag = false flag false for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            } int idx = 0; int idx = 0; idx 0 idx < arrayLen-1-iter idx arrayLen-1-iter arrayLen-1-iter arrayLen 1 iter idx+=1 idx+=1 idx 1 {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            } {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            } if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                } arr[idx] > arr[idx+1] arr[idx] arr idx arr[idx+1] arr idx+1 idx 1 {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                } count += 1 count 1 int tmp = arr[idx]; tmp arr[idx] arr idx arr[idx] = arr[idx+1] arr[idx] arr idx arr[idx+1] arr idx+1 idx 1 arr[idx+1] = tmp arr[idx+1] arr idx+1 idx 1 tmp flag = true flag true printArray() printArray return count; count public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    } main {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    } Main bubbleSort = new Main(); bubbleSort new Main() Scanner sc = new Scanner(System.in); sc new Scanner(System.in) bubbleSort.init(sc) bubbleSort.init bubbleSort sc int res = bubbleSort.bubbleSort(); res bubbleSort.bubbleSort() bubbleSort.bubbleSort bubbleSort System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main {

    private int arrayLen;
    private int arr[];

    private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    }

    private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    }

    private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    }

    public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    }
} public class Main {

    private int arrayLen;
    private int arr[];

    private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    }

    private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    }

    private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    }

    public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    }
} Main
import java.util.Scanner;

/**
 * Created by zhangrunfeng on 2019-04-30
 */
public class Main {

    private int arrayLen;
    private int arr[];

    private void init(Scanner sc) {
        arrayLen = sc.nextInt();
        arr = new int[arrayLen];
        for (int idx = 0; idx < arrayLen; idx++) {
            arr[idx] = sc.nextInt();
        }
    }

    private void printArray() {
        for (int idx = 0; idx < arrayLen-1; idx++) {
            System.out.print(arr[idx] + " ");
        }
        System.out.println(arr[arrayLen-1]);
    }

    private int bubbleSort() {
        int count = 0;

        boolean flag = true;
        for (int iter = 0; iter < arrayLen-1; iter+=1) {
            if (!flag) break;
            flag = false;

            for (int idx = 0; idx < arrayLen-1-iter; idx+=1) {
                if (arr[idx] > arr[idx+1]) {
                    count += 1;

                    // swap arr[idx] and arr[idx+1]
                    int tmp = arr[idx];
                    arr[idx] = arr[idx+1];
                    arr[idx+1] = tmp;
                    flag = true;
                }
            }

        }

        printArray();

        return count;
    }

    public static void main(String[] args) {
        Main bubbleSort = new Main();
        Scanner sc = new Scanner(System.in);
        bubbleSort.init(sc);
        int res = bubbleSort.bubbleSort();
        System.out.println(res);
    }
}

