Skip to content

Commit 637140b

Browse files
authored
Merge pull request deutranium#85 from pandeyxamit/bubble-sort
Added Readme file for Bubble Sort.
2 parents d2a1b16 + 51f4628 commit 637140b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

sortingAlgo/bubbleSort/Readme.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
```

0 commit comments

Comments
 (0)