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:
Alex Klymenko
2024-06-29 23:33:40 +03:00
committed by GitHub
parent 20e7a3aca4
commit 758df7dcc3
2 changed files with 29 additions and 3 deletions

View File

@ -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;
}
}
/**