Bubble Sort : Java Sorting Program Code with Example

Sorting is  to place elements in increasing or decreasing order.  Bubble sort is also known as sinking sort .
In this sorting algorithm , we keep comparing the adjacent pair , if they are in not right order , then they swapped each other position . When there are no elements  swapped in one full iteration of element list , then it indicates that bubble sort is completed.
Read Also  :       Selection Sort in Java


When to Use

Bubble sort is good to use for small number of input elements(less than 1000) . You can see  by running the below algorithm for
(Input)(Space)                           (Time taken)
10 elements           :           Bubble sort took   0  milliseconds
100 elements         :           Bubble sort took   1  milliseconds
10000 elements     :           Bubble sort took    181 milliseconds
100000 elements   :           Bubble sort took    19096 milliseconds


So , you can see it is not wise decision to use Bubble sort for large number of elements .


Read Also  :       Insertion Sort in Java

Big O Notation

In simple words , Big O allows us to say something about how the size of inputs affect the runtime of a program .This technique can also be applied to other resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on time or space or expected time or space that an algorithm takes up .

Properties

It is a  Stable algorithm i.e does not change the relative order of elements with equal keys

Best , Average,Worst Cases

*  Best Case        :    O(n)
* Average Case   :    O(n^2)
* Worst Case      :    O(n^2)
* Worst Case (space complexity) :  O(1)


Read Also :      Merge Sort in simple words with Example

                         Quick Sort Java Code with Example


Demo here :

Bubble sort java program



































Code is given below :



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

public class BubbleSort {
    
    public static int[] bubbleSort(int[] list) {
        for (int i = (list.length - 1); i >= 0; i--) {
            for (int j = 1; j <= i; j++) {
                if (list[j - 1] > list[j]) {
                    // swap elements at j-1 and j
                    int temp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = temp;
                }
            }
        }
        
        return list;
    }
    
    
    public static void main(String args[]) throws Exception
    {
        String list="";
        int i=0,n=0;
        
        BubbleSort s= new BubbleSort();
        ArrayList<Integer> arrlist=new ArrayList<Integer>();
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Please enter the list of elements,one element per line");
        System.out.println(" write 'STOP' when list is completed ");
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        while(!(list=bf.readLine()).equalsIgnoreCase("stop")){
            int intelement=Integer.parseInt(list);
            arrlist.add(intelement);
            
        }
        
        int elementlist[]  = new int[arrlist.size()];
        Iterator<Integer> iter = arrlist.iterator();
        for (int j=0;iter.hasNext();j++) {
            elementlist[j] = iter.next();
        }
        
        elementlist=bubbleSort(elementlist);
        System.out.println(" ");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Values after Bubble Sort : ");
        for (int j=0;j<elementlist.length;j++) {
            System.out.println(elementlist[j]+" ");
        }
    }
}

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry