-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeSort.js
60 lines (48 loc) · 1.96 KB
/
MergeSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Sort from '../Sort';
export default class MergeSort extends Sort {
sort(originalArray) {
// Call visiting callback.
this.callbacks.visitingCallback(null);
// If array is empty or consists of one element then return this array since it is sorted.
if (originalArray.length <= 1) {
return originalArray;
}
// Split array on two halves.
const middleIndex = Math.floor(originalArray.length / 2);
const leftArray = originalArray.slice(0, middleIndex);
const rightArray = originalArray.slice(middleIndex, originalArray.length);
// Sort two halves of split array
const leftSortedArray = this.sort(leftArray);
const rightSortedArray = this.sort(rightArray);
// Merge two sorted arrays into one.
return this.mergeSortedArrays(leftSortedArray, rightSortedArray);
}
mergeSortedArrays(leftArray, rightArray) {
const sortedArray = [];
// 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) {
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;
} else {
minElement = rightArray[rightIndex];
// Increment index pointer to the right
rightIndex += 1;
}
// Add the minimum element to the sorted array.
sortedArray.push(minElement);
// Call visiting callback.
this.callbacks.visitingCallback(minElement);
}
// 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));
}
}