mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 10:15:51 +08:00
feat: optimize SortUtils.swap
by skipping operations for equal indices (#5266)
* Refactor: adding check to swap method in SortUtils * Checkstyle: fix formatting * Checkstyle: fix formatting, and redundant braces * fix: adding flipped tests, removed messages from tests * checkstyle: fix indent * style: mark `temp` as `final` * tests: remove test case with empty array Such calls should be excluded. --------- Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
@ -17,9 +17,11 @@ final class 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) {
|
||||
final T temp = array[i];
|
||||
array[i] = array[j];
|
||||
array[j] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user