|
| 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); |
0 commit comments