translation: Update chapter_sorting / bubble_sort.md (#1646)

* translation: Update chapter_sorting / bubble_sort.md

* Update bubble_sort.md

line 3 updated. 'reminiscent of' - > 'like'

* Update bubble_sort.md

* Update bubble_sort.md

* Update bubble_sort.md

* Update bubble_sort.md
This commit is contained in:
Phoenix Xie
2025-03-09 09:29:03 +11:00
committed by GitHub
parent 18a5fec836
commit 2fcdd499ea

View File

@ -1,8 +1,8 @@
# Bubble sort
<u>Bubble sort</u> achieves sorting by continuously comparing and swapping adjacent elements. This process resembles bubbles rising from the bottom to the top, hence the name bubble sort.
<u>Bubble sort</u> works by continuously comparing and swapping adjacent elements. This process is like bubbles rising from the bottom to the top, hence the name "bubble sort."
As shown in the figure below, the bubbling process can be simulated using element swap operations: starting from the leftmost end of the array and moving right, sequentially compare the size of adjacent elements. If "left element > right element," then swap them. After the traversal, the largest element will be moved to the far right end of the array.
As shown in the figure below, the bubbling process can be simulated using element swaps: start from the leftmost end of the array and move right, comparing each pair of adjacent elements. If the left element is greater than the right element, swap them. After the traversal, the largest element will have bubbled up to the rightmost end of the array.
=== "<1>"
![Simulating bubble process using element swap](bubble_sort.assets/bubble_operation_step1.png)
@ -27,12 +27,12 @@ As shown in the figure below, the bubbling process can be simulated using elemen
## Algorithm process
Assuming the length of the array is $n$, the steps of bubble sort are shown in the figure below.
Assume the array has length $n$. The steps of bubble sort are shown in the figure below:
1. First, perform a "bubble" on $n$ elements, **swapping the largest element to its correct position**.
2. Next, perform a "bubble" on the remaining $n - 1$ elements, **swapping the second largest element to its correct position**.
3. Similarly, after $n - 1$ rounds of "bubbling," **the top $n - 1$ largest elements will be swapped to their correct positions**.
4. The only remaining element is necessarily the smallest and does not require sorting, thus the array sorting is complete.
1. First, perform one "bubble" pass on $n$ elements, **swapping the largest element to its correct position**.
2. Next, perform a "bubble" pass on the remaining $n - 1$ elements, **swapping the second largest element to its correct position**.
3. Continue in this manner; after $n - 1$ such passes, **the largest $n - 1$ elements will have been moved to their correct positions**.
4. The only remaining element **must** be the smallest, so **no** further sorting is required. At this point, the array is sorted.
![Bubble sort process](bubble_sort.assets/bubble_sort_overview.png)
@ -44,9 +44,9 @@ Example code is as follows:
## Efficiency optimization
We find that if no swaps are performed in a round of "bubbling," the array is already sorted, and we can return the result immediately. Thus, we can add a flag `flag` to monitor this situation and return immediately when it occurs.
If no swaps occur during a round of "bubbling," the array is already sorted, so we can return immediately. To detect this, we can add a `flag` variable; whenever no swaps are made in a pass, we set the flag and return early.
Even after optimization, the worst-case time complexity and average time complexity of bubble sort remain at $O(n^2)$; however, when the input array is completely ordered, it can achieve the best time complexity of $O(n)$.
Even with this optimization, the worst time complexity and average time complexity of bubble sort remains $O(n^2)$. However, if the input array is already sorted, the best-case time complexity can be as low as $O(n)$.
```src
[file]{bubble_sort}-[class]{}-[func]{bubble_sort_with_flag}
@ -54,6 +54,6 @@ Even after optimization, the worst-case time complexity and average time complex
## Algorithm characteristics
- **Time complexity of $O(n^2)$, adaptive sorting**: The length of the array traversed in each round of "bubbling" decreases sequentially from $n - 1$, $n - 2$, $\dots$, $2$, $1$, totaling $(n - 1) n / 2$. With the introduction of `flag` optimization, the best time complexity can reach $O(n)$.
- **Space complexity of $O(1)$, in-place sorting**: Only a constant amount of extra space is used by pointers $i$ and $j$.
- **Stable sorting**: As equal elements are not swapped during the "bubbling".
- **Time complexity of $O(n^2)$, adaptive sorting.** Each round of "bubbling" traverses array segments of length $n - 1$, $n - 2$, $\dots$, $2$, $1$, which sums to $(n - 1) n / 2$. With a `flag` optimization, the best-case time complexity can reach $O(n)$ when the array is already sorted.
- **Space complexity of $O(1)$, in-place sorting.** Only a constant amount of extra space is used by pointers $i$ and $j$.
- **Stable sorting.** Because equal elements are not swapped during "bubbling," their original order is preserved, making this a stable sort.