package hackerrank.ctci;

import java.util.Scanner;

/**
 * https://www.hackerrank.com/challenges/ctci-bubble-sort
 */
public class BubbleSort {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // TODO
    }
}

@@ -147,108 +147,1270 @@
    p
-ublic static void main(String%5B%5D args) %7B%0A        Scanner in = new Scanner(System.in);%0A        // TODO
+rivate static void bubbleSort(int%5B%5D a) %7B%0A        int numberOfSwaps = 0;%0A        for (int i = 0; i %3C a.length; i++) %7B%0A            // Track number of elements swapped during a single array traversal%0A            for (int j = 0; j %3C a.length - 1; j++) %7B%0A                // Swap adjacent elements if they are in decreasing order%0A                if (a%5Bj%5D %3E a%5Bj + 1%5D) %7B%0A                    swap(a, j, j + 1);%0A                    numberOfSwaps++;%0A                %7D%0A            %7D%0A%0A            // If no elements were swapped during a traversal, array is sorted%0A            if (numberOfSwaps == 0) %7B%0A                break;%0A            %7D%0A        %7D%0A        System.out.println(String.format(%22Array is sorted in %25d swaps.%22, numberOfSwaps));%0A        System.out.println(String.format(%22First Element: %25d%22, a%5B0%5D));%0A        System.out.println(String.format(%22Last Element: %25d%22, a%5Ba.length - 1%5D));%0A    %7D%0A%0A    private static void swap(int%5B%5D a, int i, int j) %7B%0A        int tmp = a%5Bi%5D;%0A        a%5Bi%5D = a%5Bj%5D;%0A        a%5Bj%5D = tmp;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Scanner in = new Scanner(System.in);%0A        int n = in.nextInt();%0A        int%5B%5D a = new int%5Bn%5D;%0A        for (int i = 0; i %3C n; i++) %7B%0A            a%5Bi%5D = in.nextInt();%0A        %7D%0A        bubbleSort(a);
 %0A   

Solve Bubble Sort