Class BubbleSentinel
java.lang.Object
topics.sorting.bubble.BubbleSentinel
- All Implemented Interfaces:
SortingAlgorithm
Bubble Sort (Optimized with Sentinel)
An optimized variant of the left-bubbling sort. It introduces a "sentinel" boolean flag to monitor if any mathematical swaps occurred during the current array traversal. If a full pass completes without any swaps, the algorithm deduces that the array is already completely sorted and terminates early.
Complexity Analysis
- Time Complexity (Worst/Average):
O(N²)- Occurs when the array is in reverse order. - Time Complexity (Best):
O(N)- Achieved directly due to the Sentinel flag if the array is already sorted. - Space Complexity:
O(1)- Sorts entirely in-place. - Stability: Yes - Maintains the relative order of equal elements.
- Author:
- vicegd
- See Also:
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
BubbleSentinel
public BubbleSentinel()
-
-
Method Details
-
sort
public void sort(int[] elements) Description copied from interface:SortingAlgorithmSorts the elements in-place silently.- Specified by:
sortin interfaceSortingAlgorithm- Parameters:
elements- The array of integers to be sorted.
-
sort
public void sort(int[] elements, boolean trace) Description copied from interface:SortingAlgorithmSorts the elements in-place, optionally emitting step-by-step traces to visualize the algorithmic progression.- Specified by:
sortin interfaceSortingAlgorithm- Parameters:
elements- The array of integers to be sorted.trace- Iftrue, logs the internal state during execution.
-