-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
41 lines (34 loc) · 1 KB
/
BubbleSort.java
File metadata and controls
41 lines (34 loc) · 1 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
package Sorting;
/**
* Time Complexity: Worst & Average - O(N^2), Best - O(N), Auxiliary Space: O(1)
* It is "Stable Sort" as it does not change the order of the duplicate items.
* It is also "In Place" Algorithm.
*/
public class BubbleSort {
static void bubbleSort(int[] arr) {
int n = arr.length;
// Outer For Loop
for (int i = 0; i < n - 1; i++) {
boolean flag = false;
// Inner For Loop
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
flag = true;
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
// Optimizing the Approach
if (!flag)
return;
}
}
public static void main(String[] args) {
int[] arr = {7, 4, 5, 1, 2};
bubbleSort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
}