Skip to content

Commit a48f4c0

Browse files
authored
Update README.md
1 parent 7f6a7fc commit a48f4c0

File tree

1 file changed

+65
-1
lines changed
  • 07 - Sorting Algorithms Problems/02 - Bubble Sort Algorithm Problems/01 - Example

1 file changed

+65
-1
lines changed

07 - Sorting Algorithms Problems/02 - Bubble Sort Algorithm Problems/01 - Example/README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,73 @@
1-
<h1 align='center'>Example</h1>
1+
<h1 align='center'>Bubble - Sort - Algorithm - Example</h1>
22

33
## Problem Statement
44

55
Write a function that sorts an array of integers in ascending order using the Bubble Sort algorithm. Implement the algorithm in C++ and ensure that the function stops early if the array becomes sorted before all passes are complete.
66

7+
## Problem Explanation
8+
The **Bubble Sort** algorithm is a simple comparison-based sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The algorithm gets its name because smaller elements "bubble" to the top (beginning) of the list while larger elements "sink" to the bottom (end).
9+
10+
### Steps of the Bubble Sort Algorithm:
11+
12+
1. **Start from the beginning** of the list.
13+
2. **Compare adjacent elements**:
14+
- If the current element is greater than the next one, **swap** them.
15+
- If the current element is not greater than the next one, move to the next pair.
16+
3. **Repeat the process** for each pair of adjacent elements in the list.
17+
4. **Pass through the entire list**. After the first pass, the largest element is guaranteed to have "bubbled" to the last position.
18+
5. **Repeat** the process for the remaining unsorted part of the list (ignoring the last sorted element).
19+
6. **Stop when no more swaps are needed**, meaning the list is sorted.
20+
21+
### Example:
22+
23+
Let’s sort the list `[5, 3, 8, 4, 2]` using Bubble Sort:
24+
25+
**Pass 1:**
26+
- Compare `5` and `3`: Swap → `[3, 5, 8, 4, 2]`
27+
- Compare `5` and `8`: No swap → `[3, 5, 8, 4, 2]`
28+
- Compare `8` and `4`: Swap → `[3, 5, 4, 8, 2]`
29+
- Compare `8` and `2`: Swap → `[3, 5, 4, 2, 8]`
30+
31+
At the end of the first pass, `8` is at its correct position.
32+
33+
**Pass 2:**
34+
- Compare `3` and `5`: No swap → `[3, 5, 4, 2, 8]`
35+
- Compare `5` and `4`: Swap → `[3, 4, 5, 2, 8]`
36+
- Compare `5` and `2`: Swap → `[3, 4, 2, 5, 8]`
37+
38+
At the end of the second pass, `5` is in its correct position.
39+
40+
**Pass 3:**
41+
- Compare `3` and `4`: No swap → `[3, 4, 2, 5, 8]`
42+
- Compare `4` and `2`: Swap → `[3, 2, 4, 5, 8]`
43+
44+
At the end of the third pass, `4` is in its correct position.
45+
46+
**Pass 4:**
47+
- Compare `3` and `2`: Swap → `[2, 3, 4, 5, 8]`
48+
49+
At the end of the fourth pass, the list is sorted: `[2, 3, 4, 5, 8]`.
50+
51+
### Characteristics of Bubble Sort:
52+
53+
- **Time Complexity**:
54+
- Worst-case: **O(n²)** (when the array is sorted in reverse order)
55+
- Best-case: **O(n)** (when the array is already sorted)
56+
- Average-case: **O(n²)**
57+
58+
- **Space Complexity**: **O(1)** (since it's an in-place sorting algorithm)
59+
60+
- **Stable Sorting**: Yes, Bubble Sort is a stable sorting algorithm, meaning that equal elements retain their relative order.
61+
62+
- **Adaptive**: In the best case (already sorted list), Bubble Sort can run in linear time **O(n)** if you add an optimization to detect if a pass doesn't require any swaps.
63+
64+
### Advantages:
65+
- Simple to understand and implement.
66+
- Works well on small datasets or nearly sorted data.
67+
68+
### Disadvantages:
69+
- Inefficient for large datasets (due to its O(n²) time complexity).
70+
771
## Problem Solution
872
```cpp
973
#include <iostream>

0 commit comments

Comments
 (0)