-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBubbleSort.java
39 lines (36 loc) · 1.18 KB
/
BubbleSort.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class BubbleSort
{
static void bubbleSort(int[] a)
{
int n = a.length;
int temp;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args)
{
int a[] ={28,12,55,33,65,878,2,34,54};
System.out.println("Before Bubble Sort");
for(int i=0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
bubbleSort(a);
System.out.println("After Bubble Sort");
for(int i=0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
}
}