-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA3_BubbleSort.java
More file actions
35 lines (25 loc) · 948 Bytes
/
DSA3_BubbleSort.java
File metadata and controls
35 lines (25 loc) · 948 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
30
31
32
33
34
35
import java.util.Arrays;
public class DSA3_BubbleSort {
public static void main(String[] args) {
int [] arr = {2,4,7,8,10,1};
Bubble(arr);
System.out.println(Arrays.toString(arr));
}
static void Bubble(int arr[]){
boolean swapped; // if array is sorted for that not complete all loops and break as the array is sorted
for (int index = 0; index < arr.length; index++) {
swapped =false;
for (int j = 1; j < arr.length-index; j++) {
if (arr[j] < arr[j-1]){
int temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
swapped = true ;
}
}
if (!swapped){
break;
}
}
}
}