import java.io.*;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

    int[] arr;

    int size;

    int swaps;

    public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    }

    private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    }

    private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    }


    private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    }


    class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    }
}
 import java.io.*; io.* java import java.util.List; List java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {

    int[] arr;

    int size;

    int swaps;

    public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    }

    private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    }

    private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    }


    private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    }


    class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    }
} Main int[] arr; arr int size; size int swaps; swaps public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    } main {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    } Main g = new Main(); g new Main() g.readArray() g.readArray g g.bubbleSort() g.bubbleSort g g.printArray() g.printArray g System.out.println("\n" + g.swaps) System.out.println System.out System System.out "\n" + g.swaps "\n" g.swaps g g.swaps String[] args args private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    } readArray {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    } InputReader reader = new InputReader(System.in); reader new InputReader(System.in) size = reader.nextInt() size reader.nextInt() reader.nextInt reader arr = new int[size] arr new int[size] size for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt(); int i=0; int i=0; i 0 i<size i size ++i ++i i arr[i] = reader.nextInt(); arr[i] = reader.nextInt() arr[i] arr i reader.nextInt() reader.nextInt reader private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    } printArray {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    } PrintWriter writer = new PrintWriter(System.out); writer new PrintWriter(System.out) for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        } int i=0; int i=0; i 0 i<size i size ++i ++i i {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        } {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        } writer.write("" + arr[i]) writer.write writer "" + arr[i] "" arr[i] arr i if(i<size-1) writer.write(" "); i<size-1 i size-1 size 1 writer.write(" ") writer.write writer " " writer.flush() writer.flush writer private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    } bubbleSort {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    } boolean flag=false; flag false for(int i=0; i<size-1; ++i) {

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        } int i=0; int i=0; i 0 i<size-1 i size-1 size 1 ++i ++i i {

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        } {

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        } flag=false flag false for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            } int j=0; int j=0; j 0 j<size-1-i j size-1-i size-1-i size 1 i ++j ++j j {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            } {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            } if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                } arr[j]>arr[j+1] arr[j] arr j arr[j+1] arr j+1 j 1 {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                } flag=true flag true ++swaps swaps int tmp = arr[j]; tmp arr[j] arr j arr[j] = arr[j+1] arr[j] arr j arr[j+1] arr j+1 j 1 arr[j+1] = tmp arr[j+1] arr j+1 j 1 tmp if(!flag) break; !flag flag break; class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    } InputReader private BufferedReader reader; reader private StringTokenizer tokenizer; tokenizer InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        } InputReader {

            reader = new BufferedReader(new InputStreamReader(ip));
        } reader = new BufferedReader(new InputStreamReader(ip)) reader new BufferedReader(new InputStreamReader(ip)) InputStream ip ip String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        } next {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        } if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } tokenizer==null || !tokenizer.hasMoreTokens() tokenizer==null tokenizer null !tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    tokenizer = new StringTokenizer(reader.readLine());
                } tokenizer = new StringTokenizer(reader.readLine()) tokenizer new StringTokenizer(reader.readLine()) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer int nextInt() {

            return Integer.parseInt(next());
        } nextInt {

            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public class Main {

    int[] arr;

    int size;

    int swaps;

    public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    }

    private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    }

    private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    }


    private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    }


    class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    }
} public class Main {

    int[] arr;

    int size;

    int swaps;

    public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    }

    private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    }

    private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    }


    private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    }


    class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    }
} Main
import java.io.*;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

    int[] arr;

    int size;

    int swaps;

    public static void main(String[] args)
    {

        Main g = new Main();

        g.readArray();

        g.bubbleSort();

        g.printArray();

        System.out.println("\n" + g.swaps);
    }

    private void readArray() {

        InputReader reader = new InputReader(System.in);

        size = reader.nextInt();

        arr = new int[size];

        for(int i=0; i<size; ++i)
            arr[i] = reader.nextInt();
    }

    private void printArray() {

        PrintWriter writer = new PrintWriter(System.out);

        for(int i=0; i<size; ++i) {

            writer.write("" + arr[i]);

            if(i<size-1) writer.write(" ");
        }

        writer.flush();
    }


    private void bubbleSort() {

        boolean flag=false;

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

            flag=false;

            for(int j=0; j<size-1-i; ++j) {

                if(arr[j]>arr[j+1]) {

                    flag=true;

                    ++swaps;

                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }

            // no swap has taken place so array is already sorted at this point
            if(!flag) break;
        }

    }


    class InputReader {

        private BufferedReader reader;

        private StringTokenizer tokenizer;

        InputReader(InputStream ip) {

            reader = new BufferedReader(new InputStreamReader(ip));
        }

        String next() {

            if(tokenizer==null || !tokenizer.hasMoreTokens()) {

                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return tokenizer.nextToken();
        }

        int nextInt() {

            return Integer.parseInt(next());
        }
    }
}

