Skip to content

Commit ae433cf

Browse files
committed
How to merge sorted arrays: Renamed the variables
1 parent e9fdd2d commit ae433cf

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

  • algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays

‎algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ public class SortedArrays {
44

55
public static int[] merge(int[] first, int[] second) {
66

7-
int m = first.length;
8-
int n = second.length;
7+
int firstLength = first.length;
8+
int secondLength = second.length;
99

10-
int i, j, k;
11-
i = j = k = 0;
10+
int[] result = new int[firstLength + secondLength];
1211

13-
int[] result = new int[m + n];
12+
int firstPosition, secondPosition, resultPosition;
13+
firstPosition = secondPosition = resultPosition = 0;
1414

15-
while (i < m && j < n) {
15+
while (firstPosition < firstLength && secondPosition < secondLength) {
1616

17-
if (first[i] < second[j]) {
18-
result[k++] = first[i++];
17+
if (first[firstPosition] < second[secondPosition]) {
18+
result[resultPosition++] = first[firstPosition++];
1919
} else {
20-
result[k++] = second[j++];
20+
result[resultPosition++] = second[secondPosition++];
2121
}
2222
}
2323

24-
while (i < m) {
25-
result[k++] = first[i++];
24+
while (firstPosition < firstLength) {
25+
result[resultPosition++] = first[firstPosition++];
2626
}
2727

28-
while (j < n) {
29-
result[k++] = second[j++];
28+
while (secondPosition < secondLength) {
29+
result[resultPosition++] = second[secondPosition++];
3030
}
3131

3232
return result;

0 commit comments

Comments
 (0)