Bubble Shorting in Array

Bubble Shorting in Array

Previous Home Next

 

Following program for Bubble shorting in Array( Increasing and Decreasing order).

 

 

/*
 * Save as a BubbleShortinArray.java
 * Program for BubbleShoting in the array(Increasing and Decreasing order)
 */
package r4r.co.in;

public class BubbleShortinArray {

//Declare some element into the array.
    private static int array[] = {1, 15, 3, 11, 20, 17, 7};

    public static void main(String[] args) {

        /*
         * Add all the element into the array(Fill the Array)
         * Display it
         */
        System.out.println("Element into the Array:");
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i] + "");
        }
        {
            System.out.println("\nAfter Shorting,Element display in Increasing order:");
            for (int j = 0; j < array.length; j++) {
                for (int i = j + 1; i < array.length; i++) {
                    if (array[i] < array[j]) {
                        int temp = array[j];
                        array[j] = array[i];
                        array[i] = temp;
                    }
                }
                //Display the Element
                System.out.println(array[j]);
            }
        }
        System.out.println("\nAfter Shorting,Element display in Decreasing order:");
        for (int j = array.length - 1; j >= 0; j--) {
            for (int i = j - 0; i < array.length; i++) {
                if (array[i] < array[j]) {
                    int temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                }
            }//Display the Element
            System.out.println(array[j]);
        }
    }
}

Element into the Array:
1
15
3
11
20
17
7

After Shorting,Element display in Increasing order:
1
3
7
11
15
17
20

After Shorting,Element display in Decreasing order:
20
17
15
11
7
3
1

Previous Home Next