Class Bubble

java.lang.Object
topics.sorting.bubble.Bubble
All Implemented Interfaces:
SortingAlgorithm

public class Bubble extends Object implements SortingAlgorithm

Bubble Sort (Left-Bubbling)

Educational sorting implementation without early-termination optimizations. This specific variant iterates backwards, causing the smallest elements to "bubble" up to the beginning (left side) of the array with each pass.

Algorithm Steps

  1. Establish a boundary for the sorted portion at the beginning of the array.
  2. Start at the very end of the array and iterate backwards to the sorted boundary.
  3. Compare the current element with the element immediately before it.
  4. If they are out of order (previous > current), swap them.
  5. After each full pass, the absolute minimum of the unsorted portion is guaranteed to be placed at the sorted boundary.
  6. Repeat strictly N-1 times.

Complexity Analysis

  • Time Complexity: O(N²) strictly in all cases (Best, Average, and Worst) because the early-termination flag is deliberately omitted.
  • Space Complexity: O(1) - Sorts entirely in-place.
  • Stability: Yes - Maintains relative order of equal elements.
Author:
vicegd
See Also:
  • invalid reference
    topics.sorting.SortingAlgorithm
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    sort(int[] elements)
    Sorts the elements in-place silently.
    void
    sort(int[] elements, boolean trace)
    Sorts the elements in-place, optionally emitting step-by-step traces to visualize the algorithmic progression.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Bubble

      public Bubble()
  • Method Details

    • sort

      public void sort(int[] elements)
      Description copied from interface: SortingAlgorithm
      Sorts the elements in-place silently.
      Specified by:
      sort in interface SortingAlgorithm
      Parameters:
      elements - The array of integers to be sorted.
    • sort

      public void sort(int[] elements, boolean trace)
      Description copied from interface: SortingAlgorithm
      Sorts the elements in-place, optionally emitting step-by-step traces to visualize the algorithmic progression.
      Specified by:
      sort in interface SortingAlgorithm
      Parameters:
      elements - The array of integers to be sorted.
      trace - If true, logs the internal state during execution.