Home > Article > Web Front-end > A Voyage through Algorithms using Javascript - Bubble Sort
Bubble Sort is one of the simplest sorting algorithms in computer science. Named for the way smaller elements "bubble" to the top of the list with each iteration, it's an excellent tool for teaching the basics of sorting algorithms. While not the most efficient for large datasets, its simplicity makes it a great starting point for understanding how sorting algorithms work.
Bubble Sort works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they're in the wrong order. This process is repeated until no more swaps are needed, indicating that the list is sorted.
Here's a step-by-step breakdown:
Let's visualize this process:
Recorded gif from https://visualgo.net/en/sorting
Let's examine three implementations of Bubble Sort in JavaScript, each with increasing levels of optimization.
function bubbleSort(list) { // Outer loop: iterate through the entire list for (let i = 0; i < list.length - 1; i++) { // Inner loop: compare adjacent elements for (let j = 0; j < list.length - 1; j++) { // If the current element is greater than the next one if (list[j] > list[j + 1]) { // Swap the elements using destructuring assignment [list[j], list[j + 1]] = [list[j + 1], list[j]]; } } } // Return the sorted list return list; }
This basic implementation uses nested loops to compare and swap adjacent elements. The outer loop ensures we make enough passes through the array, while the inner loop performs the comparisons and swaps.
function bubbleSort(list) { // Outer loop: iterate through the entire list for (let i = 0; i < list.length - 1; i++) { // Flag to check if any swaps occurred in this pass let swapped = false; // Inner loop: compare adjacent elements for (let j = 0; j < list.length - 1; j++) { // If the current element is greater than the next one if (list[j] > list[j + 1]) { // Swap the elements using destructuring assignment [list[j], list[j + 1]] = [list[j + 1], list[j]]; // Set the swapped flag to true swapped = true; } } // If no swaps occurred in this pass, the list is sorted if (!swapped) { break; // Exit the outer loop early } } // Return the sorted list return list; }
This version introduces a swapped flag to check if any swaps were made in a pass. If no swaps occur, the list is already sorted, and we can break out of the loop early.
function bubbleSort(list) { // Outer loop: iterate through the entire list for (let i = 0; i < list.length - 1; i++) { // Flag to check if any swaps occurred in this pass let swapped = false; // Inner loop: compare adjacent elements // Note: We reduce the upper bound by i in each pass for (let j = 0; j < list.length - 1 - i; j++) { // If the current element is greater than the next one if (list[j] > list[j + 1]) { // Swap the elements using destructuring assignment [list[j], list[j + 1]] = [list[j + 1], list[j]]; // Set the swapped flag to true swapped = true; } } // If no swaps occurred in this pass, the list is sorted if (!swapped) { break; // Exit the outer loop early } } // Return the sorted list return list; }
This final optimization reduces the range of the inner loop by i in each pass. This is because after each pass, the largest unsorted element "bubbles up" to its correct position at the end of the array.
Bubble Sort's performance characteristics are as follows:
Time Complexity:
Space Complexity: O(1) - Bubble Sort is an in-place sorting algorithm, requiring only a constant amount of additional memory.
Compared to more advanced algorithms like Quick Sort (average O(n log n)) or Merge Sort (O(n log n)), Bubble Sort's quadratic time complexity makes it inefficient for large datasets.
Advantages:
Disadvantages:
Cocktail Sort, also known as Cocktail Shake Sort or Bidirectional Bubble Sort, is an improved version of Bubble Sort. It traverses the list in both directions, helping to move elements to their correct positions more efficiently.
Cocktail Sort is particularly useful in cases where the array has elements that are initially large at the beginning and small at the end, as it can reduce the total number of passes needed compared to traditional Bubble Sort.
Here's a visualization of Cocktail Sort:
Visual from Wikipedia: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
function cocktailSort(list) { let swapped; // The do...while loop ensures the sorting continues until no swaps are needed do { // Reset the swapped flag at the beginning of each complete iteration swapped = false; // First pass: left to right (like standard bubble sort) for (let i = 0; i < list.length - 1; i++) { // If the current element is greater than the next, swap them if (list[i] > list[i + 1]) { [list[i], list[i + 1]] = [list[i + 1], list[i]]; // Mark that a swap occurred swapped = true; } } // If no swaps occurred in the first pass, the array is sorted if (!swapped) { break; // Exit the do...while loop early } // Reset the swapped flag for the second pass swapped = false; // Second pass: right to left (this is what makes it "cocktail" sort) for (let i = list.length - 2; i >= 0; i--) { // If the current element is greater than the next, swap them if (list[i] > list[i + 1]) { [list[i], list[i + 1]] = [list[i + 1], list[i]]; // Mark that a swap occurred swapped = true; } } // The loop will continue if any swaps occurred in either pass } while (swapped); // Return the sorted list return list; }
This implementation alternates between forward and backward passes through the list, potentially reducing the number of iterations needed to sort the array.
While Bubble Sort isn't typically used in production environments for large-scale applications, it can still find use in certain scenarios:
Bubble Sort, despite its inefficiencies with large datasets, offers valuable insights into the world of sorting algorithms and algorithm analysis. Its straightforward approach makes it an excellent teaching tool for beginners in computer science.
Key takeaways are:
While you're unlikely to use Bubble Sort in production code for large-scale applications, the principles behind it are fundamental to many algorithms. The process of optimizing Bubble Sort teaches valuable lessons about algorithm improvement, serving as a stepping stone to more advanced computational problem-solving.
When it comes to algorithms, there's rarely a one-size-fits-all solution. The best algorithm for a given task depends on the specific requirements, constraints, and characteristics of your data. Bubble Sort, with all its limitations, still has its place in this diverse algorithmic ecosystem.
The above is the detailed content of A Voyage through Algorithms using Javascript - Bubble Sort. For more information, please follow other related articles on the PHP Chinese website!