Loading…
Loading…
A bidirectional bubble sort: each round bubbles the largest to the right, then the smallest to the left, closing in from both ends.
Start cocktail shaker sort.
1void cocktailSort(int[] a) {2 int left = 0, right = a.length - 1;3 while (left < right) {4 for (int i = left; i < right; i++)5 if (a[i] > a[i+1]) swap(a, i, i+1);6 right--;7 for (int i = right; i > left; i--)8 if (a[i-1] > a[i]) swap(a, i-1, i);9 left++;10 }11}