Skip to content

feat: optimize SortUtils.swap by skipping operations for equal indices #5266

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 7 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 5 additions & 3 deletions src/main/java/com/thealgorithms/sorts/SortUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ private SortUtils() {
* @param <T> the type of elements in the array
*/
public static <T> void swap(T[] array, int i, int j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
if (i != j) {
T temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/thealgorithms/sorts/SortUtilsTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.thealgorithms.sorts;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class SortUtilsTest {

Expand Down Expand Up @@ -67,4 +73,23 @@ void isSortedListFalse() {
List<Integer> array3 = List.of(5, 4, 3, 2, 1);
assertFalse(SortUtils.isSorted(array3));
}

@ParameterizedTest
@MethodSource("provideArraysForSwap")
public <T> void testSwap(T[] array, int i, int j, T[] expected, String message) {
SortUtils.swap(array, i, j);
assertArrayEquals(expected, array, message);
}

private static Stream<Arguments> provideArraysForSwap() {
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."), 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."));
}

@Test
public void testSwapOutOfBounds() {
Integer[] array = {1, 2, 3, 4};
assertThrows(ArrayIndexOutOfBoundsException.class, () -> SortUtils.swap(array, -1, 4), "Swapping out of bounds should throw an ArrayIndexOutOfBoundsException.");
}
}