Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda