File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## Bubble Sort
2
+ Bubble sort is an algorithm that compares the adjacent elements and swaps their positions
3
+ if they are not in the intended order. The order can be ascending or descending.
4
+
5
+ bubbleSort(array)
6
+ swapped <- false
7
+ for i <- 1 to indexOfLastUnsortedElement-1
8
+ if leftElement > rightElement
9
+ swap leftElement and rightElement
10
+ swapped <- true
11
+ end bubbleSort
12
+
13
+ ## Complexity
14
+ Bubble Sort is one of the simplest sorting algorithms. Two loops are implemented in the algorithm.
15
+ ### Time Complexities:
16
+ * #### Worst Case Complexity: O(n<sup >2</sup >)
17
+ If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
18
+ * #### Best Case Complexities: O(n)
19
+ If the array is already sorted, then there is no need for sorting.
20
+ * #### Average Case Complexity: O(n<sup >2</sup >)
21
+ It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
22
+
23
+ ### Space Complexity:
24
+ Space Complexity is ** O(2)** because the variable swapped adds to the space complexity.
25
+ ### Bubble Sort Applications:
26
+ It is used in the following cases where
27
+ 1 . the complexity of the code does not matter.
28
+ 2 . a short code is preffered.
29
+
30
+ ### Instruction for Running code:
31
+
32
+ - Cpp
33
+ ```
34
+ g++ bubbleSort.cpp<br>
35
+ ./a.out
36
+ ```
37
+ - Python
38
+ ```
39
+ python3 bubbleSort\.py
40
+ ```
You can’t perform that action at this time.
0 commit comments