How to merge sorted arrays: Renamed the variables

This commit is contained in:
Vivek Balasubramaniam 2019-11-30 18:20:38 +05:30
parent 2f020d30ba
commit 277a1ee266
1 changed files with 13 additions and 13 deletions

View File

@ -4,29 +4,29 @@ public class SortedArrays {
public static int[] merge(int[] first, int[] second) {
int m = first.length;
int n = second.length;
int firstLength = first.length;
int secondLength = second.length;
int i, j, k;
i = j = k = 0;
int[] result = new int[firstLength + secondLength];
int[] result = new int[m + n];
int firstPosition, secondPosition, resultPosition;
firstPosition = secondPosition = resultPosition = 0;
while (i < m && j < n) {
while (firstPosition < firstLength && secondPosition < secondLength) {
if (first[i] < second[j]) {
result[k++] = first[i++];
if (first[firstPosition] < second[secondPosition]) {
result[resultPosition++] = first[firstPosition++];
} else {
result[k++] = second[j++];
result[resultPosition++] = second[secondPosition++];
}
}
while (i < m) {
result[k++] = first[i++];
while (firstPosition < firstLength) {
result[resultPosition++] = first[firstPosition++];
}
while (j < n) {
result[k++] = second[j++];
while (secondPosition < secondLength) {
result[resultPosition++] = second[secondPosition++];
}
return result;