/*
 *  You are free to use this code anywhere, copy, modify and redistribute at your
 *  own risk.
 *  Your are solely responsibly for any damage that may occur by using this code.
 *
 *  This code is not tested for all boundary conditions. Use it at your own risk.
 */
package com.zhokhov.interview.sorting;

import static com.zhokhov.interview.util.Console.*;

/**
 * @author <a href='mailto:alexey@zhokhov.com'>Alexey Zhokhov</a>
 */
public class BubbleSort {

    public void sort(int[] array) {
        for (int reverseIndex = array.length - 1; reverseIndex > 1; reverseIndex--) {
            ____grey("------\nreverseIndex: " + reverseIndex);

            for (int i = 0; i < reverseIndex; i++) {
                ____grey("i: " + i);

                if (array[i] > array[i + 1]) {
                    ____blue("Swapping: " + i + " and " + (i + 1));

                    // Swap code
                    int temp = array[i];

                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
    }

    public static void main(String[] args) {
        int array[] = {7, 1, 8, 2, 0, 12, 10, 6, 5, 3};

        __yellow("\nNew array: ");
        __dump(array);

        System.out.println("\nSorting\n");

        new BubbleSort().sort(array);

        __green("\nResult: ");
        __dump(array);

        System.out.print("\n");
    }

}
@@ -466,38 +466,160 @@
    p
-ublic void sort(int%5B%5D array) %7B
+rivate int COMPARISONS_COUNT;%0A    private int SWAPS_COUNT;%0A%0A    public void sort(int%5B%5D array) %7B%0A        COMPARISONS_COUNT = 0;%0A        SWAPS_COUNT = 0;%0A
 %0A   
@@ -852,24 +852,62 @@
 i: %22 + i);%0A%0A
+                COMPARISONS_COUNT++;%0A%0A
             
@@ -937,24 +937,59 @@
 y%5Bi + 1%5D) %7B%0A
+                    SWAPS_COUNT++;%0A
             
@@ -1205,16 +1205,133 @@
  = temp;
+%0A%0A                    __red(%22%5Cn==%3E %22);%0A                    __dump(array);%0A                    System.out.println(%22%22);
 %0A       
@@ -1589,24 +1589,68 @@
     
-new BubbleSort()
+BubbleSort bubbleSort = new BubbleSort();%0A        bubbleSort
 .sor
@@ -1707,32 +1707,241 @@
 __dump(array);%0A%0A
+        ____grey(%22%5CnStatistics%22);%0A        __green(%22  Comparisons: %22);%0A        System.out.print(bubbleSort.COMPARISONS_COUNT);%0A        __green(%22%5Cn  Swaps: %22);%0A        System.out.print(bubbleSort.SWAPS_COUNT);%0A%0A
         System.o

Print array changes to console and added statistics in the end of result [Bubble Sort].