diff --git a/src/main/java/com/thealgorithms/sorts/ExchangeSort.java b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java new file mode 100644 index 000000000000..3048e26cfadd --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java @@ -0,0 +1,65 @@ +package com.thealgorithms.sorts; + +import static com.thealgorithms.sorts.SortUtils.*; + +/** + * Exchange Sort (Cocktail Shaker Sort) implementation. + * + * @author 555vedant(Vedant Kasar) + * @see SortAlgorithm + */ +class ExchangeSort implements SortAlgorithm { + + /** + * Implements generic exchange sort (Cocktail Shaker Sort) algorithm. + * + * @param array the array to be sorted. + * @param the type of elements in the array. + * @return the sorted array. + */ + @Override + public > T[] sort(T[] array) { + int left = 0; + int right = array.length - 1; + + while (left <= right) { + boolean swapped = false; + + // Traverse from left to right + for (int i = left; i < right; ++i) { + if (greater(array[i], array[i + 1])) { + swap(array, i, i + 1); + swapped = true; + } + } + + // If no swap occurred, the array is already sorted + if (!swapped) { + break; + } + + // Move the right boundary one position to the left + --right; + + // Traverse from right to left + for (int i = right; i > left; --i) { + if (greater(array[i - 1], array[i])) { + swap(array, i - 1, i); + swapped = true; + } + } + + // If no swap occurred, the array is already sorted + if (!swapped) { + break; + } + + // Move the left boundary one position to the right + ++left; + } + + return array; + } +} + + diff --git a/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java b/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java new file mode 100644 index 000000000000..6c4271fa9e19 --- /dev/null +++ b/src/test/java/com/thealgorithms/sorts/ExchangeSortTest.java @@ -0,0 +1,8 @@ +package com.thealgorithms.sorts; + +public class ExchangeSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new ExchangeSort(); + } +}