Skip to content

Commit 10fc822

Browse files
committed
feat(js): add modular solution and documentation for normal challenge 2 - Merge Sort implementation
1 parent acae77a commit 10fc822

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Challenge Description and Solution
2+
3+
## English Version
4+
5+
### Challenge Description
6+
Develop a function that implements the Merge Sort algorithm to sort an array. Evaluate the efficiency of your implementation and consider cases with large arrays.
7+
8+
### Code Explanation
9+
Merge Sort is a divide-and-conquer algorithm that divides the array into halves, recursively sorts each half, and then merges the sorted halves.
10+
11+
The `mergeSort` static method recursively splits the array until subarrays have one element. The `merge` static method then combines two sorted arrays into one sorted array.
12+
13+
### Relevant Code Snippet
14+
15+
```javascript
16+
class MergeSort {
17+
static mergeSort(arr) {
18+
if (arr.length <= 1) {
19+
return arr;
20+
}
21+
const mid = Math.floor(arr.length / 2);
22+
const left = MergeSort.mergeSort(arr.slice(0, mid));
23+
const right = MergeSort.mergeSort(arr.slice(mid));
24+
return MergeSort.merge(left, right);
25+
}
26+
27+
static merge(left, right) {
28+
const result = [];
29+
let i = 0, j = 0;
30+
while (i < left.length && j < right.length) {
31+
if (left[i] < right[j]) {
32+
result.push(left[i]);
33+
i++;
34+
} else {
35+
result.push(right[j]);
36+
j++;
37+
}
38+
}
39+
return result.concat(left.slice(i)).concat(right.slice(j));
40+
}
41+
}
42+
```
43+
44+
### Example Usage
45+
46+
```javascript
47+
import MergeSort from './mergeSort.js';
48+
49+
const sampleArray = [38, 27, 43, 3, 9, 82, 10];
50+
console.log("Original array:", sampleArray);
51+
const sortedArray = MergeSort.mergeSort(sampleArray);
52+
console.log("Sorted array:", sortedArray);
53+
```
54+
55+
---
56+
57+
## Versión en Español
58+
59+
### Descripción del Reto
60+
Desarrolla una función que implemente el algoritmo Merge Sort para ordenar un arreglo. Evalúa la eficiencia de tu implementación y considera casos con arreglos grandes.
61+
62+
### Explicación del Código
63+
Merge Sort es un algoritmo de divide y vencerás que divide el arreglo en mitades, ordena recursivamente cada mitad y luego combina las mitades ordenadas.
64+
65+
El método estático `mergeSort` divide recursivamente el arreglo hasta que los subarreglos tienen un elemento. El método estático `merge` combina dos arreglos ordenados en uno solo ordenado.
66+
67+
### Fragmento de Código Relevante
68+
69+
```javascript
70+
class MergeSort {
71+
static mergeSort(arr) {
72+
if (arr.length <= 1) {
73+
return arr;
74+
}
75+
const mid = Math.floor(arr.length / 2);
76+
const left = MergeSort.mergeSort(arr.slice(0, mid));
77+
const right = MergeSort.mergeSort(arr.slice(mid));
78+
return MergeSort.merge(left, right);
79+
}
80+
81+
static merge(left, right) {
82+
const result = [];
83+
let i = 0, j = 0;
84+
while (i < left.length && j < right.length) {
85+
if (left[i] < right[j]) {
86+
result.push(left[i]);
87+
i++;
88+
} else {
89+
result.push(right[j]);
90+
j++;
91+
}
92+
}
93+
return result.concat(left.slice(i)).concat(right.slice(j));
94+
}
95+
}
96+
```
97+
98+
### Ejemplo de Uso
99+
100+
```javascript
101+
import MergeSort from './mergeSort.js';
102+
103+
const sampleArray = [38, 27, 43, 3, 9, 82, 10];
104+
console.log("Arreglo original:", sampleArray);
105+
const sortedArray = MergeSort.mergeSort(sampleArray);
106+
console.log("Arreglo ordenado:", sortedArray);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
/*
3+
Challenge: Develop a function that implements the Merge Sort algorithm to sort an array.
4+
Evaluate the efficiency of your implementation and consider cases with large arrays.
5+
*/
6+
7+
// main.js - Example usage of MergeSort
8+
9+
import MergeSort from './mergeSort.js';
10+
11+
function main() {
12+
const sampleArray = [38, 27, 43, 3, 9, 82, 10];
13+
console.log("Original array:", sampleArray);
14+
const sortedArray = MergeSort.mergeSort(sampleArray);
15+
console.log("Sorted array:", sortedArray);
16+
}
17+
18+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// mergeSort.js - Merge Sort implementation
2+
3+
class MergeSort {
4+
static mergeSort(arr) {
5+
if (arr.length <= 1) {
6+
return arr;
7+
}
8+
const mid = Math.floor(arr.length / 2);
9+
const left = MergeSort.mergeSort(arr.slice(0, mid));
10+
const right = MergeSort.mergeSort(arr.slice(mid));
11+
return MergeSort.merge(left, right);
12+
}
13+
14+
static merge(left, right) {
15+
const result = [];
16+
let i = 0, j = 0;
17+
while (i < left.length && j < right.length) {
18+
if (left[i] < right[j]) {
19+
result.push(left[i]);
20+
i++;
21+
} else {
22+
result.push(right[j]);
23+
j++;
24+
}
25+
}
26+
return result.concat(left.slice(i)).concat(right.slice(j));
27+
}
28+
}
29+
30+
export default MergeSort;

0 commit comments

Comments
 (0)