DECMATRIX

Mathematical Tools

Bubble Sort
i
Enter up to 21 integer numbers from -999 to 999, separated by commas.
5
0
1
-3
8
2
2
3
4
-2
2
5

Steps:

Run the algorithm to see the recorded steps.

No steps recorded yet.
SlowFast

Code

Bubble Sort is a simple sorting algorithm that repeatedly walks through the list to be sorted, comparing each pair of adjacent elements and swapping them if they are in the wrong order. This process is repeated until the list is sorted. The name 'Bubble Sort' comes from the fact that larger elements 'bubble' to the top of the list with each iteration.

Complexity: In the classic version, the time complexity is always O(n²) in all scenarios (worst, average, and best case). Since this algorithm has no early-stop mechanism, even if the list is already completely sorted (best case), it will still perform all passes and comparisons in a 'blind' way, wasting processing time.

1const bubbleSort = (arr) => {
2    // Outer loop ensures the sweep happens N times
3    for (let i = 0; i < arr.length; i++) {
4        // Inner loop ignores the last already sorted elements
5        for (let j = 0; j < arr.length - i - 1; j++) {
6            // If the current is greater than the next, swap!
7            if (arr[j] > arr[j + 1]) {
8                let temp = arr[j];
9                arr[j] = arr[j + 1];
10                arr[j + 1] = temp;
11            }
12        }
13    }
14    return arr;
15};

Definition of Bubble Sort

Bubble Sort is a simple sorting algorithm that repeatedly walks through the list to be sorted. It compares each pair of adjacent elements and swaps them if they are in the wrong order. The process is repeated until the entire list is sorted. The name 'Bubble Sort' comes from the fact that larger elements 'bubble' to the end of the list with each iteration.

How Bubble Sort Works

  1. Start by comparing the first two elements of the list.
  2. If the first element is greater than the second, swap them.
  3. Move to the next elements and repeat the process.
  4. Continue until you have walked through the entire list without needing to make any more swaps.
  5. The list is sorted when no more swaps are needed.

Applications and Real-World Use

Although it is one of the most famous and studied algorithms in programming, Bubble Sort is rarely implemented in production systems, real-world software, or standard libraries. Its quadratic nature O(n²) makes it extremely slow as the amount of data grows. Its main value is didactic: it is the perfect tool to introduce students to sorting concepts, array iteration, and performance analysis, serving as an essential foundation for understanding more efficient algorithms.

Properties of Bubble Sort

  1. Time complexity: The worst and average cases of Bubble Sort have a time complexity of O(n²), where n is the number of elements in the list. The best case, when the list is already sorted, has a time complexity of O(n) due to the need to walk through the list to verify that it is sorted.
  2. Space complexity: Bubble Sort is an in-place sorting algorithm, which means it does not require significant additional space beyond what is needed to store the original list. Therefore, its space complexity is O(1).
  3. Stability: Bubble Sort is a stable sorting algorithm, which means it preserves the relative order of equal elements. If two elements have the same value, they will remain in the same order as in the original list after sorting.

FAQ

01. Why is Bubble Sort considered an inefficient algorithm?

02. What is the difference between Classic and Optimized Bubble Sort?

03. Does Bubble Sort need extra memory to run?

04. When should I choose Bubble Sort in a real project?

Decmatrix - Your open-source mathematical toolkit for calculations, conversions, and more.
Terms of Use
Privacy Policy
Contact: decimatrix25@gmail.com