You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Illustrates a simplesorting 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.'
4
4
Subjects:
5
5
- 'Code Foundations'
6
6
- 'Computer Science'
7
-
- 'Interview Prep'
8
7
Tags:
9
8
- 'Algorithms'
9
+
- 'Arrays'
10
10
- 'Bubble Sort'
11
11
- 'Sorting Algorithms'
12
12
CatalogContent:
13
13
- 'learn-java'
14
14
- 'paths/computer-science'
15
15
---
16
16
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.
18
18
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?
20
27
21
28
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:
22
29
@@ -30,45 +37,116 @@ Bubble Sort is a straightforward algorithm that sorts an array by repeatedly com
30
37
31
38
The process is characterized by the larger elements "bubbling" up to the end of the array with each pass, hence the name "Bubble Sort".
32
39
33
-
### Implementation
40
+
Here is an image that shows the Bubble Sort Algorithm in action:
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]`.
34
45
35
-
The following pseudocode demonstrates the Bubble Sort Algorithm:
46
+
## Pseudocode for Bubble Sort
36
47
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
+
publicclassBubbleSort {
68
+
publicstaticvoidmain(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
+
publicstaticvoidbubbleSort(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
+
}
47
104
```
48
105
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:
50
107
51
-
## Time Complexity
108
+
```shell
109
+
Sorted array:
110
+
1 2 5 5 6 9
111
+
```
52
112
53
-
Overall Time Complexity: O(n^2)
113
+
##Time Complexity of Bubble Sort
54
114
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.
58
120
59
121
> **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.
60
122
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 |
-`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.
67
145
68
-
##Example and Illustration
146
+
### 2. What is the main disadvantage of Bubble Sort?
69
147
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.
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