diff --git a/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java b/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java index 5a6ba256..b02168f6 100644 --- a/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java @@ -45,7 +45,7 @@ public class DualPivotQuickSort implements SortAlgorithm { */ private static > int[] partition(T[] array, int left, int right) { if (array[left].compareTo(array[right]) > 0) { - swap(array, left, right); + SortUtils.swap(array, left, right); } T pivot1 = array[left]; @@ -58,7 +58,7 @@ public class DualPivotQuickSort implements SortAlgorithm { while (less <= great) { // If element is less than pivot1 if (array[less].compareTo(pivot1) < 0) { - swap(array, less, left++); + SortUtils.swap(array, less, left++); } // If element is greater or equal to pivot2 @@ -67,10 +67,10 @@ public class DualPivotQuickSort implements SortAlgorithm { great--; } - swap(array, less, great--); + SortUtils.swap(array, less, great--); if (array[less].compareTo(pivot1) < 0) { - swap(array, less, left++); + SortUtils.swap(array, less, left++); } } @@ -79,19 +79,13 @@ public class DualPivotQuickSort implements SortAlgorithm { j--; great++; // Bring the pivots to their appropriate positions - swap(array, left, j); - swap(array, right, great); + SortUtils.swap(array, left, j); + SortUtils.swap(array, right, great); // return the pivots' indices return new int[] {less, great}; } - private static > void swap(T[] array, int left, int right) { - T temp = array[left]; - array[left] = array[right]; - array[right] = temp; - } - /** * Main method * diff --git a/src/main/java/com/thealgorithms/sorts/ExchangeSort.java b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java index 28303430..67e94b88 100644 --- a/src/main/java/com/thealgorithms/sorts/ExchangeSort.java +++ b/src/main/java/com/thealgorithms/sorts/ExchangeSort.java @@ -32,16 +32,10 @@ class ExchangeSort implements SortAlgorithm { for (int i = 0; i < array.length - 1; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i].compareTo(array[j]) > 0) { - swap(array, i, j); + SortUtils.swap(array, i, j); } } } return array; } - - private void swap(T[] array, int i, int j) { - T temp = array[i]; - array[i] = array[j]; - array[j] = temp; - } } diff --git a/src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java b/src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java index 930bb02c..32d942dc 100644 --- a/src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java +++ b/src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java @@ -16,12 +16,6 @@ public class IntrospectiveSort implements SortAlgorithm { return a; } - private static > void swap(T[] a, int i, int j) { - T temp = a[i]; - a[i] = a[j]; - a[j] = temp; - } - private static > void introSort(T[] a, int low, int high, int depth) { while (high - low > INSERTION_SORT_THRESHOLD) { if (depth == 0) { @@ -37,16 +31,16 @@ public class IntrospectiveSort implements SortAlgorithm { private static > int partition(T[] a, int low, int high) { int pivotIndex = low + (int) (Math.random() * (high - low + 1)); - swap(a, pivotIndex, high); + SortUtils.swap(a, pivotIndex, high); T pivot = a[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { if (a[j].compareTo(pivot) <= 0) { i++; - swap(a, i, j); + SortUtils.swap(a, i, j); } } - swap(a, i + 1, high); + SortUtils.swap(a, i + 1, high); return i + 1; } @@ -67,7 +61,7 @@ public class IntrospectiveSort implements SortAlgorithm { heapify(a, i, high - low + 1, low); } for (int i = high; i > low; i--) { - swap(a, low, i); + SortUtils.swap(a, low, i); heapify(a, low, i - low, low); } } @@ -83,7 +77,7 @@ public class IntrospectiveSort implements SortAlgorithm { largest = right; } if (largest != i) { - swap(a, i, largest); + SortUtils.swap(a, i, largest); heapify(a, largest, n, low); } } diff --git a/src/main/java/com/thealgorithms/sorts/SelectionSort.java b/src/main/java/com/thealgorithms/sorts/SelectionSort.java index e43df7fe..cc7b3cc6 100644 --- a/src/main/java/com/thealgorithms/sorts/SelectionSort.java +++ b/src/main/java/com/thealgorithms/sorts/SelectionSort.java @@ -1,7 +1,5 @@ package com.thealgorithms.sorts; -import static com.thealgorithms.sorts.SortUtils.swap; - public class SelectionSort implements SortAlgorithm { /** @@ -22,7 +20,7 @@ public class SelectionSort implements SortAlgorithm { } } if (minIndex != i) { - swap(arr, i, minIndex); + SortUtils.swap(arr, i, minIndex); } } return arr; diff --git a/src/main/java/com/thealgorithms/sorts/SimpleSort.java b/src/main/java/com/thealgorithms/sorts/SimpleSort.java index 7aab7c78..110bc6e4 100644 --- a/src/main/java/com/thealgorithms/sorts/SimpleSort.java +++ b/src/main/java/com/thealgorithms/sorts/SimpleSort.java @@ -9,9 +9,7 @@ public class SimpleSort implements SortAlgorithm { for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { if (SortUtils.less(array[j], array[i])) { - T element = array[j]; - array[j] = array[i]; - array[i] = element; + SortUtils.swap(array, i, j); } } } diff --git a/src/main/java/com/thealgorithms/sorts/SlowSort.java b/src/main/java/com/thealgorithms/sorts/SlowSort.java index dcd426b3..38a5fa45 100644 --- a/src/main/java/com/thealgorithms/sorts/SlowSort.java +++ b/src/main/java/com/thealgorithms/sorts/SlowSort.java @@ -20,9 +20,7 @@ public class SlowSort implements SortAlgorithm { sort(array, i, m); sort(array, m + 1, j); if (SortUtils.less(array[j], array[m])) { - T temp = array[j]; - array[j] = array[m]; - array[m] = temp; + SortUtils.swap(array, j, m); } sort(array, i, j - 1); } diff --git a/src/main/java/com/thealgorithms/sorts/SwapSort.java b/src/main/java/com/thealgorithms/sorts/SwapSort.java index 08ce9885..e0fa7087 100644 --- a/src/main/java/com/thealgorithms/sorts/SwapSort.java +++ b/src/main/java/com/thealgorithms/sorts/SwapSort.java @@ -18,9 +18,7 @@ public class SwapSort implements SortAlgorithm { int amountSmallerElements = this.getSmallerElementCount(array, index); if (amountSmallerElements > 0 && index != amountSmallerElements) { - T element = array[index]; - array[index] = array[amountSmallerElements]; - array[amountSmallerElements] = element; + SortUtils.swap(array, index, amountSmallerElements); } else { index++; }