Bubble Sorting in Java

Time Complexity :
Average and Worst case is O(n^2)
Best case is O(n)


/**
 * A simple class to demonstrate the bubble sorting
 */
public class BubbleSorting {

    public static void main(String[] args) {

        int[] intArray = {11, 2, 13, 4, 5, 7};
        BubbleSorting bubbleSorting = new BubbleSorting();
        bubbleSorting.printElements("Before Sorting", intArray);
        bubbleSorting.doSorting(intArray);
        bubbleSorting.printElements("After Sorting", intArray);
    }

    public void printElements(String message, int[] intArray) {
        System.out.println(message);
        for (Integer num : intArray) {
            System.out.println(num);
        }
    }

    /**
     * Sort the elements with Bubble sort algorithm
     * 
     * Large element will be bubbled to its proper position after each pass
     * Worst and Avg case complexity is O(n^2)
     * Best case is O(n)
     *
     * @param intArray
     */
    public static void doSorting(int[] intArray) {

        boolean isSwaped;
        int tmp;
        for (int i = 0; i < intArray.length - 1; i++) {
            isSwaped = false;
            for (int j = 1; j  intArray[j]) {
                    tmp = intArray[j];
                    intArray[j] = intArray[j - 1];
                    intArray[j - 1] = tmp;
                    isSwaped = true;
                }
            }
            if (!isSwaped) {
                System.out.println("No Elements are sorted in this pass.Hence its sorted already.Hence break the loop");
                break;
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s