Skip to content

Commit 33f7207

Browse files
authored
Merge pull request #15 from AbhinandanAdhikari/java-bubblesort
Added Java program for BubbleSort
2 parents 9b77675 + 4a6b4e8 commit 33f7207

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

BubbleSort.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.lang.*;
2+
public class BubbleSort{
3+
public static void main(String arg[])
4+
{
5+
int a[]={30,60,35,20,45,32,50};
6+
System.out.println("Array Before Sorting");
7+
for(int i=0;i<a.length;i++)
8+
{
9+
System.out.print(a[i]+" ");
10+
}
11+
System.out.println();
12+
bubbleSort(a);
13+
System.out.println("Array after sorting");
14+
for(int i=0;i<a.length;i++)
15+
{
16+
System.out.print(a[i]+" ");
17+
}
18+
}
19+
static void bubbleSort(int[] a){
20+
int n = a.length;
21+
int t = 0;
22+
for(int i=0;i<n;i++)
23+
{
24+
for(int j=1;j<n-i;j++)
25+
{
26+
if(a[j-1]>a[j])
27+
{
28+
t = a[j-1];
29+
a[j-1] = a[j];
30+
a[j]= t;
31+
}
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)