Skip to content

Commit 422cd99

Browse files
committed
Add Bubble Sort
1 parent 1034378 commit 422cd99

File tree

7 files changed

+222
-1
lines changed

7 files changed

+222
-1
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Main.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1+
import sort.SortingAlgorithms;
2+
import sort.SortingService;
3+
4+
import java.util.Arrays;
5+
16
public class Main {
27
public static void main(String[] args) {
3-
System.out.println("Hello world!");
8+
int[] arr = {5, 3, 4, 2, 1};
9+
10+
SortingService.sort(arr, SortingAlgorithms.BUBBLE_SORT);
11+
12+
System.out.println(Arrays.toString(arr));
413
}
514
}

src/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Sorting Algorithms In Java
2+
3+
1. Bubble Sort
4+
2. Selection Sort
5+
3. Insertion Sort

src/sort/SortingAlgorithms.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package sort;
2+
3+
public enum SortingAlgorithms {
4+
BUBBLE_SORT,
5+
SELECTION_SORT,
6+
INSERTION_SORT
7+
}

src/sort/SortingService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sort;
2+
3+
import sort.bubble.BubbleSort;
4+
5+
public class SortingService {
6+
public static void sort(int[] arr) {
7+
BubbleSort.sort(arr);
8+
}
9+
10+
public static void sort(int[] arr, SortingAlgorithms sortAlgorithms) {
11+
switch (sortAlgorithms) {
12+
case BUBBLE_SORT:
13+
BubbleSort.sort(arr);
14+
break;
15+
}
16+
}
17+
}

src/sort/bubble/BubbleSort.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sort.bubble;
2+
3+
public class BubbleSort {
4+
public static void sort(int[] arr) {
5+
int n = arr.length;
6+
7+
// loop for n-1 passes
8+
for (int i = 0; i < (n - 1); i++) {
9+
10+
// optimization for already sorted array
11+
boolean swapped = false;
12+
13+
// loop for all the adjacent elements
14+
for (int j = 0; j < (n - i - 1); j++) {
15+
// compare adjacent element
16+
// swap if out of order
17+
if (arr[j] > arr[j + 1]) {
18+
int temp = arr[j];
19+
arr[j] = arr[j + 1];
20+
arr[j + 1] = temp;
21+
22+
// swapped happened means array is not sorted
23+
swapped = true;
24+
}
25+
}
26+
27+
if (!swapped)
28+
return;
29+
}
30+
}
31+
}

src/sort/bubble/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Bubble Sort
2+
3+
### Time Complexity
4+
5+
---
6+
1. Worst Case : O(n<sup>2</sup>). The worst case occurs when we want to sort a list in ascending order, but it is arranged in descending order.
7+
2. Best Case : O(n). The average case is when the list is arranged in a jumbled order.
8+
3. Average Case : O(n). The best case occurs when the list is already arranged in the desired order.
9+
10+
### Space Complexity
11+
12+
---
13+
1. Space complexity is O(1) because an extra variable is used for swapping.
14+
2. In the optimized bubble sort algorithm, two extra variables are used. Hence, the space complexity will be O(2).
15+
16+
### Properties
17+
1. Comparison Based
18+
2. Stable
19+
3. Inplace
20+
21+
### Advantages
22+
1. Bubble sort is easy to understand and implement.
23+
2. It does not require any additional memory space.
24+
3. It is a stable sorting algorithm, meaning that elements with the same key value maintain their relative order in the sorted output.
25+
26+
### Disadvantages
27+
1. Bubble sort has a time complexity of O(n<sup>2</sup>) which makes it very slow for large data sets.
28+
2. Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to determine the relative order of elements in the input data set. It can limit the efficiency of the algorithm in certain cases.

0 commit comments

Comments
 (0)