-
Notifications
You must be signed in to change notification settings - Fork 91
/
BubbleSort.java
55 lines (46 loc) · 1.43 KB
/
BubbleSort.java
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
46
47
48
49
50
51
52
53
54
55
/**
* BubbleSort: sort an array by repeatedly swapping the adjacent elements if
* they are in wrong order.
*
* Worst and Average Case Time Complexity: O(n^2).
* Best Case Time Complexity: O(n).
* Space Complexity: O(1)
*
*/
import java.util.Arrays;
public class BubbleSort {
public static void sort(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-i-1; j++)
if (arr[j] > arr[j+1]) swap(arr, j, j+1);
}
}
public static void sortEarlyStop(int[] arr) {
boolean swapped = false;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr, j, j+1);
swapped = true;
}
}
if (swapped == false) break;
}
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) {
int[] arr1 = {10, 3, 7, 5, 1, 15, 20};
BubbleSort.sortEarlyStop(arr1);
System.out.println(Arrays.toString(arr1));
int[] arr2 = {};
BubbleSort.sortEarlyStop(arr2);
System.out.println(Arrays.toString(arr2));
int[] arr3 = {10};
BubbleSort.sortEarlyStop(arr3);
System.out.println(Arrays.toString(arr3));
}
}