@gabrielrufino/cerebrum 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Sort/MergeSort.js +36 -0
- package/package.json +1 -1
- package/src/Sort/MergeSort.spec.ts +8 -0
- package/src/Sort/MergeSort.ts +38 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MergeSort = void 0;
|
|
4
|
+
const Sort_1 = require("./Sort");
|
|
5
|
+
class MergeSort extends Sort_1.Sort {
|
|
6
|
+
execute() {
|
|
7
|
+
if (this.numbers.length <= 1) {
|
|
8
|
+
return [...this.numbers];
|
|
9
|
+
}
|
|
10
|
+
const middle = Math.floor(this.numbers.length / 2);
|
|
11
|
+
const left = this.numbers.slice(0, middle);
|
|
12
|
+
const right = this.numbers.slice(middle);
|
|
13
|
+
return this.merge(new MergeSort(left).execute(), new MergeSort(right).execute());
|
|
14
|
+
}
|
|
15
|
+
merge(left, right) {
|
|
16
|
+
const result = [];
|
|
17
|
+
let leftIndex = 0;
|
|
18
|
+
let rightIndex = 0;
|
|
19
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
20
|
+
if (left[leftIndex] < right[rightIndex]) {
|
|
21
|
+
result.push(left[leftIndex]);
|
|
22
|
+
leftIndex += 1;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
result.push(right[rightIndex]);
|
|
26
|
+
rightIndex += 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [
|
|
30
|
+
...result,
|
|
31
|
+
...left.slice(leftIndex),
|
|
32
|
+
...right.slice(rightIndex)
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.MergeSort = MergeSort;
|
package/package.json
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Sort } from './Sort';
|
|
2
|
+
|
|
3
|
+
export class MergeSort extends Sort {
|
|
4
|
+
public execute(): Array<number> {
|
|
5
|
+
if (this.numbers.length <= 1) {
|
|
6
|
+
return [...this.numbers]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const middle = Math.floor(this.numbers.length / 2)
|
|
10
|
+
const left = this.numbers.slice(0, middle)
|
|
11
|
+
const right = this.numbers.slice(middle)
|
|
12
|
+
|
|
13
|
+
return this.merge(new MergeSort(left).execute(), new MergeSort(right).execute())
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
private merge(left: number[], right: number[]): number[] {
|
|
17
|
+
const result: number[] = []
|
|
18
|
+
|
|
19
|
+
let leftIndex = 0
|
|
20
|
+
let rightIndex = 0
|
|
21
|
+
|
|
22
|
+
while (leftIndex < left.length && rightIndex < right.length) {
|
|
23
|
+
if (left[leftIndex] < right[rightIndex]) {
|
|
24
|
+
result.push(left[leftIndex])
|
|
25
|
+
leftIndex += 1
|
|
26
|
+
} else {
|
|
27
|
+
result.push(right[rightIndex])
|
|
28
|
+
rightIndex += 1
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return [
|
|
33
|
+
...result,
|
|
34
|
+
...left.slice(leftIndex),
|
|
35
|
+
...right.slice(rightIndex)
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
}
|