Skip to content

Commit 57fddd1

Browse files
author
Alex Klymenko
committed
Refactor: adding check to swap method in SortUtils
1 parent 20e7a3a commit 57fddd1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ private SortUtils() {
1717
* @param <T> the type of elements in the array
1818
*/
1919
public static <T> void swap(T[] array, int i, int j) {
20-
T temp = array[i];
21-
array[i] = array[j];
22-
array[j] = temp;
20+
if (i != j) {
21+
T temp = array[i];
22+
array[i] = array[j];
23+
array[j] = temp;
24+
}
2325
}
2426

2527
/**

src/test/java/com/thealgorithms/sorts/SortUtilsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.thealgorithms.sorts;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
46
import static org.junit.jupiter.api.Assertions.assertTrue;
57

68
import java.util.List;
9+
import java.util.stream.Stream;
10+
711
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.params.ParameterizedTest;
13+
import org.junit.jupiter.params.provider.Arguments;
14+
import org.junit.jupiter.params.provider.MethodSource;
815

916
class SortUtilsTest {
1017

@@ -67,4 +74,24 @@ void isSortedListFalse() {
6774
List<Integer> array3 = List.of(5, 4, 3, 2, 1);
6875
assertFalse(SortUtils.isSorted(array3));
6976
}
77+
78+
@ParameterizedTest
79+
@MethodSource("provideArraysForSwap")
80+
public <T> void testSwap(T[] array, int i, int j, T[] expected, String message) {
81+
SortUtils.swap(array, i, j);
82+
assertArrayEquals(expected, array, message);
83+
}
84+
85+
private static Stream<Arguments> provideArraysForSwap() {
86+
return Stream.of(Arguments.of(new Integer[]{1, 2, 3, 4}, 1, 2, new Integer[]{1, 3, 2, 4}, "Swapping adjacent elements should work correctly."), Arguments.of(new Integer[]{1, 2, 3, 4}, 0, 3, new Integer[]{4, 2, 3, 1}, "Swapping non-adjacent elements should work correctly."), Arguments.of(new Integer[]{1, 2, 3, 4}, 2, 2, new Integer[]{1, 2, 3, 4}, "Swapping the same index should not change the array."),
87+
Arguments.of(new String[]{"a", "b", "c", "d"}, 0, 3, new String[]{"d", "b", "c", "a"}, "Swapping first and last elements should work correctly."), Arguments.of(new String[]{null, "b", "c", null}, 0, 3, new String[]{null, "b", "c", null}, "Swapping null elements should work correctly."), Arguments.of(new Integer[]{}, 0, 0, new Integer[]{}, "Swapping in an empty array should not throw an error."));
88+
}
89+
90+
@Test
91+
public void testSwapOutOfBounds() {
92+
Integer[] array = {1, 2, 3, 4};
93+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
94+
SortUtils.swap(array, -1, 4);
95+
}, "Swapping out of bounds should throw an ArrayIndexOutOfBoundsException.");
96+
}
7097
}

0 commit comments

Comments
 (0)