From 2c74ced8af09e90f60ae45ccbd656e23f3fc64f0 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Sat, 28 Nov 2020 16:43:29 +0100 Subject: [PATCH] Fix ESLint issues with MergeSort. --- .../sorting/merge-sort/MergeSort.js | 35 ++++++++----------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/algorithms/sorting/merge-sort/MergeSort.js b/src/algorithms/sorting/merge-sort/MergeSort.js index b6eb73f0..23fca74e 100644 --- a/src/algorithms/sorting/merge-sort/MergeSort.js +++ b/src/algorithms/sorting/merge-sort/MergeSort.js @@ -1,4 +1,4 @@ -import Sort from "../Sort"; +import Sort from '../Sort'; export default class MergeSort extends Sort { sort(originalArray) { @@ -26,38 +26,33 @@ export default class MergeSort extends Sort { mergeSortedArrays(leftArray, rightArray) { const sortedArray = []; - // Use array pointers to exclude old elements after they have been added to the sorted array + // Use array pointers to exclude old elements after they have been added to the sorted array. let leftIndex = 0; let rightIndex = 0; while (leftIndex < leftArray.length && rightIndex < rightArray.length) { - // Find the minimum element between the left and right array - if ( - this.comparator.lessThanOrEqual( - leftArray[leftIndex], - rightArray[rightIndex] - ) - ) { - sortedArray.push(leftArray[leftIndex]); + let minElement = null; + // Find the minimum element between the left and right array. + if (this.comparator.lessThanOrEqual(leftArray[leftIndex], rightArray[rightIndex])) { + minElement = leftArray[leftIndex]; // Increment index pointer to the right leftIndex += 1; - - // Call visiting callback. - this.callbacks.visitingCallback(leftArray[leftIndex]); } else { - sortedArray.push(rightArray[rightIndex]); - + minElement = rightArray[rightIndex]; // Increment index pointer to the right rightIndex += 1; - - // Call visiting callback. - this.callbacks.visitingCallback(rightArray[rightIndex]); } + + // Add the minimum element to the sorted array. + sortedArray.push(minElement); + + // Call visiting callback. + this.callbacks.visitingCallback(minElement); } - // There will be one element remaining from either the left OR the right - // Concatenate the remaining element into the sorted array + // There will be elements remaining from either the left OR the right + // Concatenate the remaining elements into the sorted array return sortedArray .concat(leftArray.slice(leftIndex)) .concat(rightArray.slice(rightIndex));