We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6d19bbf commit 2c4be94Copy full SHA for 2c4be94
Sorting Algorithms/Bubble_Sort.java
@@ -1,7 +1,16 @@
1
+/*
2
+Bubble Sort is the simplest sorting algorithm
3
+that works by repeatedly swapping the adjacent
4
+elements if they are in wrong order.
5
+
6
7
+*/
8
public class BubbleSortExample {
9
static void bubbleSort(int[] arr) {
10
+ // finding array size.
11
int n = arr.length;
12
int temp = 0;
13
+ //each iteration place max element of 1 to n-i index at index n-i
14
for(int i=0; i < n; i++){
15
for(int j=1; j < (n-i); j++){
16
if(arr[j-1] > arr[j]){
@@ -32,3 +41,8 @@ public static void main(String[] args) {
32
41
33
42
}
34
43
44
45
46
+Bubble Sort always runs O(n^2) time even if the array is sorted.
47
+It can be optimized by stopping the algorithm if inner loop didn’t cause any swap.
48
0 commit comments