-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.java
More file actions
33 lines (30 loc) · 904 Bytes
/
BubbleSort.java
File metadata and controls
33 lines (30 loc) · 904 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
public class BubbleSort {
public int[] sort(int[] array) {
if (array == null || array.length < 2)
return array;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1])
swap(array, j, j + 1);
}
}
return array;
}
int[] swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
return array;
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++)
System.out.print(array[i] + " ");
System.out.println();
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
int[] array = {6, 1, 3, 2, 9, 0, 4, 5, 8, 7};
bs.sort(array);
bs.print(array);
}
}