Skip to content

Commit 6d19bbf

Browse files
authored
Add Bubble_Sort.java
Bubble_Sort implementation in java.
1 parent 4ed0b64 commit 6d19bbf

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Sorting Algorithms/Bubble_Sort.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class BubbleSortExample {
2+
static void bubbleSort(int[] arr) {
3+
int n = arr.length;
4+
int temp = 0;
5+
for(int i=0; i < n; i++){
6+
for(int j=1; j < (n-i); j++){
7+
if(arr[j-1] > arr[j]){
8+
//swap elements
9+
temp = arr[j-1];
10+
arr[j-1] = arr[j];
11+
arr[j] = temp;
12+
}
13+
}
14+
}
15+
16+
}
17+
public static void main(String[] args) {
18+
int arr[] ={3,60,35,2,45,320,5};
19+
20+
System.out.println("Array Before Bubble Sort");
21+
for(int i=0; i < arr.length; i++){
22+
System.out.print(arr[i] + " ");
23+
}
24+
System.out.println();
25+
26+
bubbleSort(arr);//sorting array elements using bubble sort
27+
28+
System.out.println("Array After Bubble Sort");
29+
for(int i=0; i < arr.length; i++){
30+
System.out.print(arr[i] + " ");
31+
}
32+
33+
}
34+
}

0 commit comments

Comments
 (0)