-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBubbleSort.java
More file actions
30 lines (25 loc) · 920 Bytes
/
BubbleSort.java
File metadata and controls
30 lines (25 loc) · 920 Bytes
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
import java.util.Arrays;
public class BubbleSort {
public static void printMyArray(int array[]) {
for(int i=0;i<array.length;i++){
System.out.print(array[i]+ ",");
}
}
public static void main(String[] args) {
int array [] = {7,8,3,12,21,14,2,90,5,16,12,1};
// Bubble Sort
for(int i= 0 ;i<array.length;i++){
// loop for comparing adjacent element
for(int j = 0; j<array.length-i-1;j++) {
// comparing if right side of current element is smaller is yes swap it
if(array[j]>array[j+1]){
int temp = array[j];
array[j]= array[j+1];
array[j+1]= temp;
}
}
}
// System.out.println(Arrays.toString(array)); // This is converting array into string
printMyArray(array);
}
}