-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmergeSort.js
More file actions
115 lines (101 loc) · 3.73 KB
/
mergeSort.js
File metadata and controls
115 lines (101 loc) · 3.73 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Callback function to measure item weights of merged arrays.
*
* @callback getItemWeightCallback
* @param {object} item The item to weight
* @returns {number} Returns item's weight.
*/
/**
* Merges array of sorted arrays into one using call back function for comparison.
*
* @param {object[][]} arrays Array of sorted arrays of objects.
* @param {getItemWeightCallback} getItemWeight Callback function to measure item weight.
* @param {boolean} ignoreDuplicates If true returns distinct weight items only.
* @returns {object[]} Returns merged sorted array.
*/
export default function mergeSort(arrays, getItemWeight, ignoreDuplicates) {
var result = null,
firstArray, secondArray, mergedArray, arrayIndex,
firstIndex, secondIndex, firstLen, secondLen, firstItem, secondItem,
firstItemWeight, secondItemWeight,
currentValue;
switch (arrays.length) {
case 0:
result = [];
break;
default:
firstArray = [];
for (arrayIndex = 0; arrayIndex < arrays.length; arrayIndex += 1) {
secondArray = arrays[arrayIndex];
mergedArray = [];
firstLen = firstArray.length;
secondLen = secondArray.length;
firstIndex = 0;
secondIndex = 0;
firstItem = null;
firstItemWeight = null;
secondItem = null;
secondItemWeight = null;
if (firstLen > 0) {
firstItem = firstArray[firstIndex];
firstItemWeight = !getItemWeight ? firstItem : getItemWeight(firstItem);
}
if (secondLen > 0) {
secondItem = secondArray[secondIndex];
secondItemWeight = !getItemWeight ? secondItem : getItemWeight(secondItem);
}
currentValue = null;
while (firstIndex < firstLen || secondIndex < secondLen) {
if (firstIndex >= firstLen) {
if (!ignoreDuplicates || currentValue != secondItem) {
mergedArray.push(secondItem);
currentValue = secondItem;
}
secondIndex += 1;
if (secondIndex < secondLen) {
secondItem = secondArray[secondIndex];
secondItemWeight = !getItemWeight ? secondItem : getItemWeight(secondItem);
}
} else {
if (secondIndex >= secondLen) {
if (!ignoreDuplicates || currentValue != firstItem) {
mergedArray.push(firstItem);
currentValue = firstItem;
}
firstIndex += 1;
if (firstIndex < firstLen) {
firstItem = firstArray[firstIndex];
firstItemWeight = !getItemWeight ? firstItem : getItemWeight(firstItem);
}
} else {
if (firstItemWeight < secondItemWeight) {
if (!ignoreDuplicates || currentValue != firstItem) {
mergedArray.push(firstItem);
currentValue = firstItem;
}
firstIndex += 1;
if (firstIndex < firstLen) {
firstItem = firstArray[firstIndex];
firstItemWeight = !getItemWeight ? firstItem : getItemWeight(firstItem);
}
} else {
if (!ignoreDuplicates || currentValue != secondItem) {
mergedArray.push(secondItem);
currentValue = secondItem;
}
secondIndex += 1;
if (secondIndex < secondLen) {
secondItem = secondArray[secondIndex];
secondItemWeight = !getItemWeight ? secondItem : getItemWeight(secondItem);
}
}
}
}
}
firstArray = mergedArray;
}
result = firstArray;
break;
}
return result;
};