-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.java
More file actions
40 lines (36 loc) · 1001 Bytes
/
BubbleSort.java
File metadata and controls
40 lines (36 loc) · 1001 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
36
37
38
39
40
public class BubbleSort {
// Soring Method
void sorting(int[] arr){
int j = 0;
int n = arr.length;
// Outer Loop
while (j != n){
// Inner loop
boolean swap = false;
for (int i =0; i < n-1-j; i++){
if (arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
swap = true;
}
}
j = (j + 1);
if (!swap){ // swap == false
break;}
}
}
// Display Method
void display(int[] arr){
for (int i : arr) { // for(i = 0,i < arr.length, i++)
System.out.print(i + " ");
}
}
// Main Method
public static void main(String[] args) {
BubbleSort obj = new BubbleSort();
int[] array = {64, 34, 25, 12, 22, 90, 11};
obj.sorting(array);
obj.display(array);
}
}