Bubble Sorting in Java

Posted By: Java Examples - 9:38 PM

Share

& Comment

Bubble Sorting is an algorithm in which we are comparing first two values and put the larger one at higher index. Then we take next two values compare these values and place larger value at higher index. This process do iteratively until the largest value is not reached at last index. Then start again from zero index up to n-1 index. The algorithm follows the same steps iteratively unlit elements are not sorted


Working of bubble sort algorithm:
Say we have an array unsorted  A[0],A[1],A[2]................ A[n-1] and A[n] as input. Then the following steps are followed by bubble sort algorithm to sort the values of an array.
 1.Compare A[0] and A[1] .
 2.If A[0]>A[1] then Swap A[0] and A[1].
 3.Take next A[1] and A[2].
 4.Comapre these values.
 5.If A[1]>A[2]  then swap A[1] and A[2]
 ...............................................................
................................................................
at last compare A[n-1] and A[n]. If A[n-1]>A[n] then swap A[n-1] and A[n]. As we see the highest value is reached at nth position. At next iteration leave nth value. Then apply the same steps repeatedly on A[0],A[1],A[2]................ A[n-1] elements repeatedly until the values of array is sorted.


In our example we are taking the following array values
5 4 3 1 2
The basic steps followed by algorithm :
In the first step compare first two values 5 and 4.
5 4 3 1 2
As 5 4 then we have to swap these values
Then the new sequence will be
4 5 3 1 2
In next step take next  two values 5and 3
4 5 3 2 1
Compare these two values .
As 5 3 then we have to swap these values.
Then the new sequence will be
4 3 5 2 1
In next step take next  two values 5 and 2
4 3 5 2 1
Compare these two values .
As 5 > 2 then we have to swap these values.
Then the new sequence will be
4 3 2 1 5
We have to follow similar steps up to end of array....
4 3 2 1 5
4 3 2 1 5
3 4 2 1 5
3  2 4 1 5
3 2 1 4 5
3 2 1 4 5
2 3 1 4 5 
............
............
1 2 3 4 5


Java version of the  Bubble Sort  is given below : 

//*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sort.bubblesort;

/**
 *
 * @author ACHCHUTHAN
 */
public class BubbleSort {
    int array[];
    int size;
   public BubbleSort(int n){
       size=n;
        //create array with size n
        array=new int[n];
        //asign random integer value into the array
        for (int i=0;i<n;i++){
            array[i]=(int) Math.round(Math.random()*92+10);
        }
   }
   
    public void Bubble_Sort() {
        int temp;
        for (int i = 0; i < size; i++) {
            for (int j = 1; j < (size - i); j++) {
                if (array[j - 1] > array[j]) {
                    temp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
    public void print(){
        System.out.println("Contents of the Array");
        for(int k=0;k<size;k++) {
            System.out.print(array[k]+" | ");
        }
        System.out.println();

    }
    public static void main(String args[]){
        BubbleSort m=new BubbleSort(10);
        System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<");
        m.print();
        m.Bubble_Sort();
        System.out.println("After Sort > > > > > > > > > > > >");
        m.print();
        System.out.println("=======+============+============+=========");
        BubbleSort m2=new BubbleSort(15);
        System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<");
        m2.print();
        m2.Bubble_Sort();
        System.out.println("After Sort > > > > > > > > > > > >");
        m2.print();
        System.out.println("=======+============+============+=========");
        BubbleSort m3=new BubbleSort(20);
        System.out.println("Before Sort <<<<<<<<<<<<<<<<<<<<<");
        m3.print();
        m3.Bubble_Sort();
        System.out.println("After Sort > > > > > > > > > > > >");
        m3.print();
        System.out.println("=======+============+===========+=========");

}
    
}

Output of this program is given bellow :

run:
Before Sort <<<<<<<<<<<<<<<<<<<<<
Contents of the Array
77 | 63 | 69 | 80 | 69 | 76 | 20 | 78 | 42 | 98 | 
After Sort > > > > > > > > > > > >
Contents of the Array
20 | 42 | 63 | 69 | 69 | 76 | 77 | 78 | 80 | 98 | 
=======+============+=======+============+=========
Before Sort <<<<<<<<<<<<<<<<<<<<<
Contents of the Array
37 | 37 | 95 | 99 | 30 | 74 | 35 | 18 | 74 | 82 | 64 | 72 | 76 | 40 | 44 | 
After Sort > > > > > > > > > > > >
Contents of the Array
18 | 30 | 35 | 37 | 37 | 40 | 44 | 64 | 72 | 74 | 74 | 76 | 82 | 95 | 99 | 
=======+============+=======+============+=========
Before Sort <<<<<<<<<<<<<<<<<<<<<
Contents of the Array
53 | 26 | 71 | 64 | 90 | 100 | 15 | 81 | 15 | 37 | 65 | 54 | 71 | 66 | 21 | 76 | 78 | 65 | 16 | 62 | 
After Sort > > > > > > > > > > > >
Contents of the Array
15 | 15 | 16 | 21 | 26 | 37 | 53 | 54 | 62 | 64 | 65 | 65 | 66 | 71 | 71 | 76 | 78 | 81 | 90 | 100 | 
=======+============+=======+============+=========
BUILD SUCCESSFUL (total time: 4 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .