-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBubbleSort.java
More file actions
60 lines (53 loc) · 1.76 KB
/
Copy pathBubbleSort.java
File metadata and controls
60 lines (53 loc) · 1.76 KB
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
56
57
58
59
60
package algorithms.sorting;
import java.util.Arrays;
/**
* created by Cenk Canarslan on 2021-01-30
*/
public class BubbleSort {
/**
* Time Complexity :
* - Worst : O(n^2), when the array is in reverse order
* - Average : O(n^2), when the array is already sorted
* - Best : O(n^2)
*
* Space Complexity :
* O(1)
*
* @param arr
*/
private void sort(int[] arr) {
System.out.println(" initial:" + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
System.out.print("(i=" + i + ",j=" + j + ")-[" + arr[j] + ":" + arr[j+1] + "]");
if (arr[j] > arr[j+1]) {
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
System.out.println("-"+Arrays.toString(arr));
}
}
}
private void sort2(int[] arr) {
System.out.println(" initial:" + Arrays.toString(arr));
for (int i = 0; i < arr.length - 2; i++) {
for (int j = 0; j < arr.length - 2 - i; j++) {
if (arr[j+1] < arr[j]) {
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
System.out.println("-"+Arrays.toString(arr));
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{64, 34, 25, 12, 22, 11, 90, 12};
// int[] arr = new int[]{7,2,6,1,3};
// int[] arr = new int[]{5,1,6,1,3};
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.sort(arr);
System.out.println(" sorted:" + Arrays.toString(arr));
}
}