Skip to content

Commit 4798de9

Browse files
committed
Swap function update to return for i == j
1 parent 92213dc commit 4798de9

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/main/java/com/thealgorithms/randomized/RandomizedQuickSort.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ private static int partition(int[] arr, int low, int high) {
5050
}
5151

5252
/**
53-
* Swaps two elements in the array.
54-
*
55-
* @param arr the array in which elements are to be swapped
56-
* @param i the first index
57-
* @param j the second index
58-
*/
53+
* Swaps two elements in the array, only if the indices are different.
54+
*
55+
* @param arr the array in which elements are to be swapped
56+
* @param i the first index
57+
* @param j the second index
58+
*/
5959
private static void swap(int[] arr, int i, int j) {
60+
if (i == j) {
61+
return;
62+
}
6063
int temp = arr[i];
6164
arr[i] = arr[j];
6265
arr[j] = temp;

0 commit comments

Comments
 (0)