From ae433cf6ec4bde50fa3bb0e05f7fe5a4d473ba41 Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sat, 30 Nov 2019 18:20:38 +0530 Subject: [PATCH] How to merge sorted arrays: Renamed the variables --- .../algorithms/sortedarrays/SortedArrays.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java index 286eaae243..7bcf049523 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java @@ -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;