-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
46 lines (34 loc) · 772 Bytes
/
BubbleSort.java
File metadata and controls
46 lines (34 loc) · 772 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
41
42
43
44
45
import java.util.Arrays;
public class BubbleSort{
private BubbleSort() {}
public static void bubbleSort(int[] A){
boolean swapped = true;
int n = A.length;
while(swapped != false){
swapped = false;
for(int i = 1; i < n; i++){
if(A[i-1] > A[i]){
int temp = A[i-1];
A[i-1] = A[i];
A[i] = temp;
swapped = true;
}
}
n = n-1;
}
}
public static void print(int[] A){
for(int i=0; i < A.length-1; i++){
System.out.print(A[i] + " ");
}
System.out.println();
}
public static void main(String[] args){
int[] A = {34, 7, 4, 123, 54, 3, 2, 111, 11, 34, 100, 99, 9};
System.out.println("Before calling sort:");
print(A);
bubbleSort(A);
System.out.println("After calling sort:");
print(A);
}
}