Replace the various swap method variants with SortUtils.swap (#5245)
Fix different variants of swap methods to SortUtils.swap Co-authored-by: AlexKlm <alx@alx.com>
This commit is contained in:
parent
8ef69bc854
commit
e8f1990c8c
@ -45,7 +45,7 @@ public class DualPivotQuickSort implements SortAlgorithm {
|
||||
*/
|
||||
private static <T extends Comparable<T>> 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 <T extends Comparable<T>> void swap(T[] array, int left, int right) {
|
||||
T temp = array[left];
|
||||
array[left] = array[right];
|
||||
array[right] = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method
|
||||
*
|
||||
|
@ -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 <T> void swap(T[] array, int i, int j) {
|
||||
T temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
}
|
||||
}
|
||||
|
@ -16,12 +16,6 @@ public class IntrospectiveSort implements SortAlgorithm {
|
||||
return a;
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> void swap(T[] a, int i, int j) {
|
||||
T temp = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = temp;
|
||||
}
|
||||
|
||||
private static <T extends Comparable<T>> 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 <T extends Comparable<T>> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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++;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user