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
Output
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
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
@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
Bubble Sort Program using array in Java
Reviewed by Admin
on
01:26
Rating:
No comments: