█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 3583

Problema 0xdd - Ordenar un arreglo pequeño

  • Autor: toroduque
  • Fecha: 2021-03-28 06:07:39 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.06 s 7 KBi
#2
Correcto
0.036 s 7 KBi
#3
Correcto
0.072 s 7 KBi
#4
Correcto
0.037 s 7 KBi
#5
Correcto
0.054 s 7 KBi
#6
Correcto
0.075 s 7 KBi
#7
Correcto
0.053 s 7 KBi
#8
Correcto
0.04 s 7 KBi
#9
Correcto
0.038 s 7 KBi
#10
Correcto
0.055 s 7 KBi
#11
Correcto
0.082 s 12 KBi
#12
Correcto
0.085 s 17 KBi
#13
Correcto
0.05 s 10 KBi
#14
Correcto
0.215 s 31 KBi
#15
Correcto
0.219 s 31 KBi
#16
Correcto
0.111 s 31 KBi
#17
Correcto
0.239 s 31 KBi
#18
Correcto
0.079 s 18 KBi
#19
Correcto
0.042 s 9 KBi
#20
Correcto
0.038 s 9 KBi
Puntos totales: 100 / 100

Código

const readline = require("readline");
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

let lineCount = 0
rl.on("line", (line) => {
    ++lineCount
   if (lineCount === 2) {
       const initial = line.split(" ").map(Number);
       const sorted = quicksort(initial);
       console.log(arrToString(sorted));
   } 
});

function quicksort(arr) {
  if (arr.length <= 1) {
    return arr;
  }

  const pivot = arr[arr.length - 1];
  const lessThanArr = [];
  const greaterThanArr = [];

  arr
    .slice(0, arr.length - 1)
    .forEach((el) =>
      el < pivot ? lessThanArr.push(el) : greaterThanArr.push(el)
    );

  return [...quicksort(lessThanArr), pivot, ...quicksort(greaterThanArr)];
}

function arrToString(arr) {
  let str = "";
  arr.forEach((el) => (str = str + " " + el));
  return str.trim();
}