Skip to content

Commit 0b50685

Browse files
authored
[Edit] General: Bubble Sort (#6990)
* [Edit] General: Bubble Sort * minor changes * fixed the image explanation * Update bubble-sort.md ---------
1 parent b608be7 commit 0b50685

File tree

1 file changed

+110
-32
lines changed

1 file changed

+110
-32
lines changed
Lines changed: 110 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
---
2-
Title: 'Bubble Sort Algorithm'
3-
Description: 'Illustrates a simple sorting algorithm using repeated swapping of adjacent elements.'
2+
Title: 'Bubble Sort'
3+
Description: 'Bubble Sort is a simple, comparison-based sorting algorithm used to arrange elements in an array in a specific order.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
7-
- 'Interview Prep'
87
Tags:
98
- 'Algorithms'
9+
- 'Arrays'
1010
- 'Bubble Sort'
1111
- 'Sorting Algorithms'
1212
CatalogContent:
1313
- 'learn-java'
1414
- 'paths/computer-science'
1515
---
1616

17-
**Bubble sort** is a simple, comparison-based [algorithm](https://www.codecademy.com/resources/docs/general/algorithm) that repeatedly steps through an [array](https://www.codecademy.com/resources/docs/java/arrays), comparing and swapping adjacent elements if they are in the wrong order. This process continues until the array is sorted, making it an intuitive method for sorting arrays.
17+
**Bubble Sort** is a simple, comparison-based algorithm for arranging elements in an [array](https://www.codecademy.com/resources/docs/java/arrays) in a specific order. This process continues until the array is sorted, making it an intuitive method for sorting arrays.
1818

19-
## BubbleSort Method
19+
**Key Characteristics:**
20+
21+
- **Simplicity**: Bubble sort is straightforward to understand and implement.
22+
- **Inefficiency for Large Lists**: Not suitable for large datasets due to its O(n^2) time complexity.
23+
- **Stability**: Bubble sort is a stable algorithm that maintains the relative order of equal elements.
24+
- **Adaptive**: Can be optimized to stop early if the list is sorted before completing all passes, making it adaptive to the initial order of elements.
25+
26+
## How Bubble Sort Works?
2027

2128
Bubble Sort is a straightforward algorithm that sorts an array by repeatedly comparing and swapping adjacent elements if they are in the wrong order. The steps of the Bubble Sort algorithm are as follows:
2229

@@ -30,45 +37,116 @@ Bubble Sort is a straightforward algorithm that sorts an array by repeatedly com
3037

3138
The process is characterized by the larger elements "bubbling" up to the end of the array with each pass, hence the name "Bubble Sort".
3239

33-
### Implementation
40+
Here is an image that shows the Bubble Sort Algorithm in action:
41+
42+
![Bubble Sort Algorithm](https://raw.githubusercontent.com/Codecademy/docs/main/media/bubble-sort.png)
43+
44+
The above image shows how Bubble Sort works step by step. It starts with the list `[5, 2, 8, 3]` and compares each pair of numbers. If the first number is bigger, they swap places. After the first round, the biggest number moves to the end. The second round compares and swaps until the next biggest number is in place. By the third round, no swaps are needed because everything is in the right order. The list is sorted as `[2, 3, 5, 8]`.
3445

35-
The following pseudocode demonstrates the Bubble Sort Algorithm:
46+
## Pseudocode for Bubble Sort
3647

37-
```plaintext
38-
Algorithm: BubbleSort(Array)
39-
for i from 0 to Array.length - 1
40-
set swapped to false
41-
for j from 0 to Array.length - i - 1
42-
if Array[j] > Array[j + 1]
43-
swap Array[j] and Array[j + 1]
44-
set swapped to true
45-
if not swapped
46-
break // Array is sorted
48+
This pseudocode accurately demonstrates the Bubble Sort Algorithm:
49+
50+
```pseudo
51+
BubbleSort(Array)
52+
for i from 0 to Array.length - 1
53+
set swapped to false
54+
for j from 0 to Array.length - i - 1
55+
if Array[j] > Array[j + 1]
56+
swap Array[j] and Array[j + 1]
57+
set swapped to true
58+
if not swapped
59+
break // Array is sorted
60+
```
61+
62+
## Implementation of Bubble Sort
63+
64+
This example implements the Bubble Sort algorithm in Java:
65+
66+
```java
67+
public class BubbleSort {
68+
public static void main(String[] args) {
69+
int[] arr = {2, 5, 1, 6, 9, 5};
70+
bubbleSort(arr);
71+
72+
System.out.println("Sorted array:");
73+
for (int num : arr) {
74+
System.out.print(num + " ");
75+
}
76+
}
77+
78+
public static void bubbleSort(int[] arr) {
79+
int n = arr.length;
80+
boolean swapped;
81+
82+
// Outer loop for each pass
83+
for (int i = 0; i < n - 1; i++) {
84+
swapped = false;
85+
86+
// Inner loop for comparing adjacent elements
87+
for (int j = 0; j < n - 1 - i; j++) {
88+
// Swap if elements are in the wrong order
89+
if (arr[j] > arr[j + 1]) {
90+
// Swap arr[j] and arr[j+1]
91+
int temp = arr[j];
92+
arr[j] = arr[j + 1];
93+
arr[j + 1] = temp;
94+
95+
swapped = true;
96+
}
97+
}
98+
99+
// If no swaps happened, the array is already sorted
100+
if (!swapped) break;
101+
}
102+
}
103+
}
47104
```
48105

49-
This pseudocode outlines the logic of the Bubble Sort algorithm without being tied to any specific programming language, making it suitable for a general algorithms document. Be sure to replace the existing Java code in your markdown document with this pseudocode snippet.
106+
Here is the output:
50107

51-
## Time Complexity
108+
```shell
109+
Sorted array:
110+
1 2 5 5 6 9
111+
```
52112

53-
Overall Time Complexity: O(n^2)
113+
## Time Complexity of Bubble Sort
54114

55-
- **Iterative Comparison**: Each pair of adjacent elements is compared, leading to n-1 comparisons in the first pass, n-2 in the second, and so on.
56-
- **Best Case Scenario**: O(n) when the array is already sorted, as only one pass will be needed.
57-
- **Worst and Average Case**: O(n^2) due to the nested loops for comparing and swapping elements.
115+
Overall Time Complexity: _O(n^2)_
116+
117+
- **Iterative Comparison**: Each pair of adjacent elements is compared, leading to `n-1` comparisons in the first pass, `n-2` in the second, and so on.
118+
- **Best Case Scenario**: _O(n)_ when the array is already sorted, as only one pass will be needed.
119+
- **Worst and Average Case**: _O(n^2)_ due to the nested loops for comparing and swapping elements.
58120

59121
> **Note:** The overall time complexity of Bubble Sort is predominantly influenced by the number of passes through the array and the comparisons within each pass.
60122
61-
## Characteristics of Bubble Sort
123+
## Space Complexity of Bubble Sort
124+
125+
Overall Time Complexity: _O(1)_
126+
127+
Bubble Sort performs with a space complexity of _O(1)_ because it's an in-place sorting algorithm that needs only a constant amount of extra memory, such as a temporary variable for swapping and loop counters. It does not use any additional data structures, making it memory-efficient despite its slower performance.
128+
129+
## Comparison with Other Sorting Algorithms
130+
131+
| Algorithm | Best Case | Average Case | Worst Case | Space Complexity |
132+
| ------------------ | ---------- | ------------ | ---------- | ---------------- |
133+
| **Bubble Sort** | O(n) | O(n²) | O(n²) | O(1) |
134+
| **Insertion Sort** | O(n) | O(n²) | O(n²) | O(1) |
135+
| **Selection Sort** | O(n²) | O(n²) | O(n²) | O(1) |
136+
| **Quick Sort** | O(n log n) | O(n log n) | O(n²) | O(log n) |
137+
| **Merge Sort** | O(n log n) | O(n log n) | O(n log n) | O(n) |
138+
| **Heap Sort** | O(n log n) | O(n log n) | O(n log n) | O(1) |
139+
140+
## Frequently Asked Questions
141+
142+
### 1. What is the main advantage of Bubble Sort?
62143

63-
- `Simplicity`: Bubble sort is straightforward to understand and implement.
64-
- `Inefficiency for Large Lists`: Not suitable for large datasets due to its O(n^2) time complexity.
65-
- `Stability`: Bubble sort is a stable algorithm, maintaining the relative order of equal elements.
66-
- `Adaptive`: Can be optimized to stop early if the list is sorted before completing all passes, making it adaptive to the initial order of elements.
144+
The primary advantage of Bubble Sort is its simplicity—it is easy to understand and implement, making it useful for teaching basic sorting concepts and algorithms.
67145

68-
## Example and Illustration
146+
### 2. What is the main disadvantage of Bubble Sort?
69147

70-
The following animation visually demonstrates the Bubble Sort Algorithm in action:
148+
The biggest disadvantage of Bubble Sort is its poor performance on large datasets due to its _O(n²)_ time complexity, which makes it inefficient compared to more advanced sorting algorithms.
71149

72-
![Bubble Sort Example](https://raw.githubusercontent.com/Codecademy/docs/main/media/bubble-sort.png)
150+
### 3. Is Bubble Sort faster than Merge Sort?
73151

74-
In this example, the numbers in the array are visually represented. With each pass through the array, the largest number in the unsorted part bubbles up to its correct position at the end of the array. This process repeats, with the range of unsorted elements shrinking each time, until the entire array is sorted.
152+
No, Bubble Sort is not faster than Merge Sort. Merge Sort consistently performs better with a time complexity of _O(n log n)_, making it significantly more efficient for large lists.

0 commit comments

Comments
 (0)