Módulo 4 · Especialización25 min
Algoritmos de Ordenamiento
Bubble Sort, Quick Sort, Merge Sort.
Recompensa al completar
Insignia “Sort master” · +30 puntos
Bubble Sort — O(n²)
javascript
function bubbleSort(array) {
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
}
}
}
return array;
}Quick Sort — O(n log n)
javascript
function quickSort(array) {
if (array.length <= 1) return array;
let pivot = array[0];
let izq = array.slice(1).filter(x => x <= pivot);
let der = array.slice(1).filter(x => x > pivot);
return [...quickSort(izq), pivot, ...quickSort(der)];
}Merge Sort — O(n log n)
javascript
function mergeSort(array) {
if (array.length <= 1) return array;
let medio = Math.floor(array.length / 2);
let izq = mergeSort(array.slice(0, medio));
let der = mergeSort(array.slice(medio));
return merge(izq, der);
}?Ejercicio
Implementa la función bubbleSort.
editor.js
12345
Recompensa al completar
Insignia “Sort master” · +30 puntos