Class BidirectionalBubble
java.lang.Object
topics.sorting.bubble.BidirectionalBubble
- All Implemented Interfaces:
SortingAlgorithm
Bidirectional Bubble Sort (Cocktail Shaker Sort)
An educational variation of the standard Bubble Sort. Instead of repeatedly passing through the list from left to right, this algorithm alternates passes from left-to-right and then right-to-left. This bidirectional approach helps to rapidly mitigate the "turtle" problem in standard Bubble Sort, where small elements at the end of the list take a long time to move to the beginning.
Complexity Analysis
- Time Complexity (Worst/Average):
O(N²)- Still scales quadratically for heavily randomized or reversed arrays. - Time Complexity (Best):
O(N)- Achieved via the early-termination sentinel 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
-
BidirectionalBubble
public BidirectionalBubble()
-
-
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.
-