Thursday, May 19, 2016

Bubble Sort using array Program in Java

Hi Guys!!
We know that there are some sorting algorithm in Data Structure under which the easiest one is bubble sort.
so lets see the bubble sort in java using array.


Implementaion of Bubble Sort Using Array

/*
 *
 @author noeik
 *
 */

public class BubbleSort {
   
    static int[] data = {10,23,45,5,3,2};// global data integer array
    public static void main(String[] args) {
       
        sort(data);
        display();   
}
//main sort method
    public static void sort(int[] data)
    {
        for(int i =0;i<data.length-1;i++)
        {
            for(int j=0;j<data.length-1-i;j++)
            {
                if(data[j]>data[j+1])
                {
                    int temp =data[j+1];
                    data[j+1] =data[j];
                    data[j] =temp;
                   
                }
            }
        }
    }
 // display method to show the array
    public static void display()
    {
        String result = "[";
        for(int d :data)
        {
            result = result +d+",";
        }
        result =result+"]";
        System.out.println(result);
    }

}

Output
[2,3,5,10,23,45,] )
Time Complexity:- The Time Complexity of Bubble Sort is O(n^2) because here in program you can see that the code is having 2 loops


In Next Post I will share the program of Selection Sort with its time complexity and all .

Thanks for Reading , Hope you Understand the code and if you have any problem do comment in Comment section below

-Noeik

0 comments:

Post a Comment