Skip to content

Replace the various swap method variants with SortUtils.swap #5245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static <T extends Comparable<T>> void dualPivotQuicksort(T[] array, int
*/
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];
Expand All @@ -58,7 +58,7 @@ private static <T extends Comparable<T>> int[] partition(T[] array, int left, in
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
Expand All @@ -67,10 +67,10 @@ else if (array[less].compareTo(pivot2) >= 0) {
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++);
}
}

Expand All @@ -79,19 +79,13 @@ else if (array[less].compareTo(pivot2) >= 0) {
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
*
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/thealgorithms/sorts/ExchangeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
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 changes: 5 additions & 11 deletions src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public <T extends Comparable<T>> T[] sort(T[] a) {
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) {
Expand All @@ -37,16 +31,16 @@ private static <T extends Comparable<T>> void introSort(T[] a, int low, int high

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;
}

Expand All @@ -67,7 +61,7 @@ private static <T extends Comparable<T>> void heapSort(T[] a, int low, int high)
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);
}
}
Expand All @@ -83,7 +77,7 @@ private static <T extends Comparable<T>> void heapify(T[] a, int i, int n, int l
largest = right;
}
if (largest != i) {
swap(a, i, largest);
SortUtils.swap(a, i, largest);
heapify(a, largest, n, low);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/thealgorithms/sorts/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.thealgorithms.sorts;

import static com.thealgorithms.sorts.SortUtils.swap;

public class SelectionSort implements SortAlgorithm {

/**
Expand All @@ -22,7 +20,7 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
}
}
if (minIndex != i) {
swap(arr, i, minIndex);
SortUtils.swap(arr, i, minIndex);
}
}
return arr;
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/thealgorithms/sorts/SimpleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
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);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/thealgorithms/sorts/SlowSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ private <T extends Comparable<T>> void sort(T[] array, int i, int j) {
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);
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/thealgorithms/sorts/SwapSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
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++;
}
Expand Down