-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
34 lines (32 loc) · 1004 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
34 lines (32 loc) · 1004 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
import java.util.Arrays;
//baapSORT
public class BubbleSort {
public static void main(String[] args) {
int[] nums = {22, 44, 55, 5, 667, 1, 46, 98,};
int[] num = {1, 2, 3, 4, 5, 6};
int[] eq = {1, 1, 2, 4, 5, 2, 0, 6, -2};
BS(nums);
BS(num);
BS(eq);
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(num));
System.out.println(Arrays.toString(eq));
}
static void BS(int[] arr) {
boolean swap;
for (int i = 0; i < arr.length; i++) {
swap = false;
for (int j = 1; j < arr.length - i; j++) {
if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
swap = true;
}
}
if (!swap) { //!false=true or swap==false
break; //break kiya if i++ loop
}
}
}
}