Skip to content

Commit 2c74ced

Browse files
committedNov 28, 2020
Fix ESLint issues with MergeSort.
1 parent 83978e9 commit 2c74ced

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed
 

‎src/algorithms/sorting/merge-sort/MergeSort.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Sort from "../Sort";
1+
import Sort from '../Sort';
22

33
export default class MergeSort extends Sort {
44
sort(originalArray) {
@@ -26,38 +26,33 @@ export default class MergeSort extends Sort {
2626
mergeSortedArrays(leftArray, rightArray) {
2727
const sortedArray = [];
2828

29-
// Use array pointers to exclude old elements after they have been added to the sorted array
29+
// Use array pointers to exclude old elements after they have been added to the sorted array.
3030
let leftIndex = 0;
3131
let rightIndex = 0;
3232

3333
while (leftIndex < leftArray.length && rightIndex < rightArray.length) {
34-
// Find the minimum element between the left and right array
35-
if (
36-
this.comparator.lessThanOrEqual(
37-
leftArray[leftIndex],
38-
rightArray[rightIndex]
39-
)
40-
) {
41-
sortedArray.push(leftArray[leftIndex]);
34+
let minElement = null;
4235

36+
// Find the minimum element between the left and right array.
37+
if (this.comparator.lessThanOrEqual(leftArray[leftIndex], rightArray[rightIndex])) {
38+
minElement = leftArray[leftIndex];
4339
// Increment index pointer to the right
4440
leftIndex += 1;
45-
46-
// Call visiting callback.
47-
this.callbacks.visitingCallback(leftArray[leftIndex]);
4841
} else {
49-
sortedArray.push(rightArray[rightIndex]);
50-
42+
minElement = rightArray[rightIndex];
5143
// Increment index pointer to the right
5244
rightIndex += 1;
53-
54-
// Call visiting callback.
55-
this.callbacks.visitingCallback(rightArray[rightIndex]);
5645
}
46+
47+
// Add the minimum element to the sorted array.
48+
sortedArray.push(minElement);
49+
50+
// Call visiting callback.
51+
this.callbacks.visitingCallback(minElement);
5752
}
5853

59-
// There will be one element remaining from either the left OR the right
60-
// Concatenate the remaining element into the sorted array
54+
// There will be elements remaining from either the left OR the right
55+
// Concatenate the remaining elements into the sorted array
6156
return sortedArray
6257
.concat(leftArray.slice(leftIndex))
6358
.concat(rightArray.slice(rightIndex));

0 commit comments

Comments
 (0)
Please sign in to comment.