Nexo is auditing two classic sorting algorithms to publish the Código con Criterio tournament leaderboard: optimized Bubble Sort (with early termination when a full pass produces no swaps) and Selection Sort.
Implement bubbleSort(puntajes, orden), selectionSort(puntajes, orden), and solve(input), which receives:
puntajes: array of 0 to 50 integers between 0 and 1000 (duplicates allowed).
orden: strictly "ASC" or "DESC".
Each sorting function must operate on its own shallow copy ([...puntajes]) without mutating the input and return { resultado, comparaciones, intercambios }.
bubbleSort(puntajes, orden) Requirements
Use an outer loop i from 0 to n - 2 and an inner loop j from 0 to n - i - 2 inclusive.
For each adjacent pair (copia[j], copia[j + 1]), increment comparaciones by 1.
Swap only if out of order (copia[j] > copia[j + 1] in "ASC", or copia[j] < copia[j + 1] in "DESC"; never swap equal values). Increment intercambios by 1 for each effective swap.
If a full pass completes without any swap (!huboIntercambio), break out of the outer loop immediately.
selectionSort(puntajes, orden) Requirements
Use an outer loop i from 0 to n - 2, initializing indiceSeleccionado = i.
Scan with j from i + 1 to n - 1, incrementing comparaciones by 1 for each element compared against copia[indiceSeleccionado].
Update indiceSeleccionado = j only when copia[j] is strictly smaller ("ASC") or strictly greater ("DESC") than copia[indiceSeleccionado].
After the inner loop finishes, swap copia[i] and copia[indiceSeleccionado]only if indiceSeleccionado !== i, incrementing intercambios by 1.
Coordination in solve(input)
Check whether input.puntajes was already sorted according to orden (yaEstabaOrdenado, where empty arrays, 1-element arrays, or sorted arrays with duplicates evaluate to true).
Run bubbleSort and selectionSort independently on input.puntajes.
Determine algoritmoMasEficienteexclusively by fewer comparaciones: "BUBBLE" if Bubble performed fewer comparisons, "SELECTION" if Selection performed fewer, or "EMPATE" if equal.
Return Value
Return an object with exactly seven properties:
puntajesOrdenados: new sorted array.
totalParticipantes: length of input.puntajes.
bubbleSort: { comparaciones, intercambios }.
selectionSort: { comparaciones, intercambios }.
algoritmoMasEficiente: "BUBBLE", "SELECTION", or "EMPATE".