Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
Want to use async functions in recursion javascript

I'm trying to implement quick sort in javascript and need await sleep() inside a function, but when I'm using async the function is not getting correct result.

On removing async from the partition function code is working fine.
Can anyone help?

async function partition(low, high) {
  var pivot = random_numbers[low];  // random_number is a global variable contains 10 random number
  var i = low+1;
  var j = high;

  while (i <= j){
    if (random_numbers[i] < pivot){
        i++;
    }else{
        swap_values(i, j);  // this will swap values in random_numbers
        swap_boxes_sq(i, j);  // this will perform some animation
        await sleep(MAX_SLEEP_TIME); // MAX_SLEEP_TIME = 2000 and sleep is defined in other file
        j--;
    }
  }

  swap_values(low, i-1);      
  return i-1;
}

function sort_q( low, high) {
  if (low < high){
    var pi = partition( low, high);
    sort_q(low, pi-1);
    sort_q(pi+1, high);
  } 
}

function quick_sort(low, high) {
  low = 0;
  high = number_of_box -1;  // number_of_box = 10
  sort_q(low, high);
  console.log(random_numbers);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You made partition asynchronous so you need to add await in sort_q (and make it asynchronous), and add await in quick_sort

The problem is that partition is async, so it returns a Promise. sort_q won't get the correct result for pi if you don't await the result of this Promise.

async function partition(low, high) {
  var pivot = random_numbers[low];  // random_number is a global variable contains 10 random number
  var i = low+1;
  var j = high;

  while (i <= j){
    if (random_numbers[i] < pivot){
        i++;
    }else{
        swap_values(i, j);  // this will swap values in random_numbers
        swap_boxes_sq(i, j);  // this will perform some animation
        await sleep(MAX_SLEEP_TIME); // MAX_SLEEP_TIME = 2000 and sleep is defined in other file
        j--;
    }
  }

  swap_values(low, i-1);      
  return i-1;
}

async function sort_q( low, high) {
  if (low < high){
    var pi = await partition( low, high);
    sort_q(low, pi-1);
    sort_q(pi+1, high);
  } 
}

async function quick_sort(low, high) {
  low = 0;
  high = number_of_box -1;  // number_of_box = 10
  await sort_q(low, high);
  console.log(random_numbers);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda