Skip to content

Commit e8f1990

Browse files
alxkmAlexKlm
and
AlexKlm
authored
Replace the various swap method variants with SortUtils.swap (#5245)
Fix different variants of swap methods to SortUtils.swap Co-authored-by: AlexKlm <[email protected]>
1 parent 8ef69bc commit e8f1990

File tree

7 files changed

+16
-42
lines changed

7 files changed

+16
-42
lines changed

src/main/java/com/thealgorithms/sorts/DualPivotQuickSort.java

+6-12
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static <T extends Comparable<T>> void dualPivotQuicksort(T[] array, int
4545
*/
4646
private static <T extends Comparable<T>> int[] partition(T[] array, int left, int right) {
4747
if (array[left].compareTo(array[right]) > 0) {
48-
swap(array, left, right);
48+
SortUtils.swap(array, left, right);
4949
}
5050

5151
T pivot1 = array[left];
@@ -58,7 +58,7 @@ private static <T extends Comparable<T>> int[] partition(T[] array, int left, in
5858
while (less <= great) {
5959
// If element is less than pivot1
6060
if (array[less].compareTo(pivot1) < 0) {
61-
swap(array, less, left++);
61+
SortUtils.swap(array, less, left++);
6262
}
6363

6464
// If element is greater or equal to pivot2
@@ -67,10 +67,10 @@ else if (array[less].compareTo(pivot2) >= 0) {
6767
great--;
6868
}
6969

70-
swap(array, less, great--);
70+
SortUtils.swap(array, less, great--);
7171

7272
if (array[less].compareTo(pivot1) < 0) {
73-
swap(array, less, left++);
73+
SortUtils.swap(array, less, left++);
7474
}
7575
}
7676

@@ -79,19 +79,13 @@ else if (array[less].compareTo(pivot2) >= 0) {
7979
j--;
8080
great++;
8181
// Bring the pivots to their appropriate positions
82-
swap(array, left, j);
83-
swap(array, right, great);
82+
SortUtils.swap(array, left, j);
83+
SortUtils.swap(array, right, great);
8484

8585
// return the pivots' indices
8686
return new int[] {less, great};
8787
}
8888

89-
private static <T extends Comparable<T>> void swap(T[] array, int left, int right) {
90-
T temp = array[left];
91-
array[left] = array[right];
92-
array[right] = temp;
93-
}
94-
9589
/**
9690
* Main method
9791
*

src/main/java/com/thealgorithms/sorts/ExchangeSort.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,10 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
3232
for (int i = 0; i < array.length - 1; i++) {
3333
for (int j = i + 1; j < array.length; j++) {
3434
if (array[i].compareTo(array[j]) > 0) {
35-
swap(array, i, j);
35+
SortUtils.swap(array, i, j);
3636
}
3737
}
3838
}
3939
return array;
4040
}
41-
42-
private <T> void swap(T[] array, int i, int j) {
43-
T temp = array[i];
44-
array[i] = array[j];
45-
array[j] = temp;
46-
}
4741
}

src/main/java/com/thealgorithms/sorts/IntrospectiveSort.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ public <T extends Comparable<T>> T[] sort(T[] a) {
1616
return a;
1717
}
1818

19-
private static <T extends Comparable<T>> void swap(T[] a, int i, int j) {
20-
T temp = a[i];
21-
a[i] = a[j];
22-
a[j] = temp;
23-
}
24-
2519
private static <T extends Comparable<T>> void introSort(T[] a, int low, int high, int depth) {
2620
while (high - low > INSERTION_SORT_THRESHOLD) {
2721
if (depth == 0) {
@@ -37,16 +31,16 @@ private static <T extends Comparable<T>> void introSort(T[] a, int low, int high
3731

3832
private static <T extends Comparable<T>> int partition(T[] a, int low, int high) {
3933
int pivotIndex = low + (int) (Math.random() * (high - low + 1));
40-
swap(a, pivotIndex, high);
34+
SortUtils.swap(a, pivotIndex, high);
4135
T pivot = a[high];
4236
int i = low - 1;
4337
for (int j = low; j <= high - 1; j++) {
4438
if (a[j].compareTo(pivot) <= 0) {
4539
i++;
46-
swap(a, i, j);
40+
SortUtils.swap(a, i, j);
4741
}
4842
}
49-
swap(a, i + 1, high);
43+
SortUtils.swap(a, i + 1, high);
5044
return i + 1;
5145
}
5246

@@ -67,7 +61,7 @@ private static <T extends Comparable<T>> void heapSort(T[] a, int low, int high)
6761
heapify(a, i, high - low + 1, low);
6862
}
6963
for (int i = high; i > low; i--) {
70-
swap(a, low, i);
64+
SortUtils.swap(a, low, i);
7165
heapify(a, low, i - low, low);
7266
}
7367
}
@@ -83,7 +77,7 @@ private static <T extends Comparable<T>> void heapify(T[] a, int i, int n, int l
8377
largest = right;
8478
}
8579
if (largest != i) {
86-
swap(a, i, largest);
80+
SortUtils.swap(a, i, largest);
8781
heapify(a, largest, n, low);
8882
}
8983
}

src/main/java/com/thealgorithms/sorts/SelectionSort.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.thealgorithms.sorts;
22

3-
import static com.thealgorithms.sorts.SortUtils.swap;
4-
53
public class SelectionSort implements SortAlgorithm {
64

75
/**
@@ -22,7 +20,7 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
2220
}
2321
}
2422
if (minIndex != i) {
25-
swap(arr, i, minIndex);
23+
SortUtils.swap(arr, i, minIndex);
2624
}
2725
}
2826
return arr;

src/main/java/com/thealgorithms/sorts/SimpleSort.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
99
for (int i = 0; i < length; i++) {
1010
for (int j = i + 1; j < length; j++) {
1111
if (SortUtils.less(array[j], array[i])) {
12-
T element = array[j];
13-
array[j] = array[i];
14-
array[i] = element;
12+
SortUtils.swap(array, i, j);
1513
}
1614
}
1715
}

src/main/java/com/thealgorithms/sorts/SlowSort.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ private <T extends Comparable<T>> void sort(T[] array, int i, int j) {
2020
sort(array, i, m);
2121
sort(array, m + 1, j);
2222
if (SortUtils.less(array[j], array[m])) {
23-
T temp = array[j];
24-
array[j] = array[m];
25-
array[m] = temp;
23+
SortUtils.swap(array, j, m);
2624
}
2725
sort(array, i, j - 1);
2826
}

src/main/java/com/thealgorithms/sorts/SwapSort.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
1818
int amountSmallerElements = this.getSmallerElementCount(array, index);
1919

2020
if (amountSmallerElements > 0 && index != amountSmallerElements) {
21-
T element = array[index];
22-
array[index] = array[amountSmallerElements];
23-
array[amountSmallerElements] = element;
21+
SortUtils.swap(array, index, amountSmallerElements);
2422
} else {
2523
index++;
2624
}

0 commit comments

Comments
 (0)