Skip to content

Commit 8e096b3

Browse files
authored
Merge pull request #114 from Omoshirio/main
commit changes
2 parents 18db73c + 8e74200 commit 8e096b3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bubblesort.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}
18+
public static void main(String[] args) {
19+
int arr[] ={3,60,35,2,45,320,5};
20+
21+
System.out.println("Array Before Bubble Sort");
22+
for(int i=0; i < arr.length; i++){
23+
System.out.print(arr[i] + " ");
24+
}
25+
System.out.println();
26+
27+
bubbleSort(arr);//sorting array elements using bubble sort
28+
29+
System.out.println("Array After Bubble Sort");
30+
for(int i=0; i < arr.length; i++){
31+
System.out.print(arr[i] + " ");
32+
}
33+
34+
}
35+
}

0 commit comments

Comments
 (0)