Skip to content

Commit 3ad77ce

Browse files
authored
Added Readme file for Bubble Sort.
1 parent 98d85f2 commit 3ad77ce

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

sortingAlgo/bubbleSort/Readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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^2$)
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^2$)
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.

0 commit comments

Comments
 (0)