refactored merge sort to use array pointers instead of .shift() (#581)

This commit is contained in:
Austin Theriot 2020-11-28 09:35:08 -06:00 committed by GitHub
parent ed52a8079e
commit 83978e9d2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import Sort from '../Sort';
import Sort from "../Sort";
export default class MergeSort extends Sort {
sort(originalArray) {
@ -24,36 +24,42 @@ export default class MergeSort extends Sort {
}
mergeSortedArrays(leftArray, rightArray) {
let sortedArray = [];
const sortedArray = [];
// In case if arrays are not of size 1.
while (leftArray.length && rightArray.length) {
let minimumElement = null;
// Use array pointers to exclude old elements after they have been added to the sorted array
let leftIndex = 0;
let rightIndex = 0;
// Find minimum element of two arrays.
if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) {
minimumElement = leftArray.shift();
} else {
minimumElement = rightArray.shift();
}
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]);
// Increment index pointer to the right
leftIndex += 1;
// Call visiting callback.
this.callbacks.visitingCallback(minimumElement);
this.callbacks.visitingCallback(leftArray[leftIndex]);
} else {
sortedArray.push(rightArray[rightIndex]);
// Push the minimum element of two arrays to the sorted array.
sortedArray.push(minimumElement);
// Increment index pointer to the right
rightIndex += 1;
// Call visiting callback.
this.callbacks.visitingCallback(rightArray[rightIndex]);
}
}
// If one of two array still have elements we need to just concatenate
// this element to the sorted array since it is already sorted.
if (leftArray.length) {
sortedArray = sortedArray.concat(leftArray);
}
if (rightArray.length) {
sortedArray = sortedArray.concat(rightArray);
}
return sortedArray;
// There will be one element remaining from either the left OR the right
// Concatenate the remaining element into the sorted array
return sortedArray
.concat(leftArray.slice(leftIndex))
.concat(rightArray.slice(rightIndex));
}
}