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

171
Visualizações
How to write a merge sort with JavaScript in Vue.Js

I am try to do a simple merge sort with JavaScript in Vue.Js. These are the functions I created. But the output can't sort the array ascendingly.

    mergeSort(arr) {
      if (arr.length <= 1) {
        this.data.arrNum = arr;
      }
      console.log("arr", arr);
      let mid = Math.floor(arr.length / 2);
      let left = arr.slice(0, mid);
      console.log("left", left);
      let right = arr.slice(mid);
      console.log("right", right);

      this.merge(left, right);
    },

    merge: function (left, right) {
      var leftIndex = 0;
      var rightIndex = 0;
      var results = [];

      while (leftIndex < left.length && rightIndex < right.length) {

        if (left[leftIndex] < right[rightIndex]) {
          results.push(left[leftIndex]);
          leftIndex++    
        } else {
          results.push(right[rightIndex]);
          rightIndex++; 
        }
      }

      results = results
        .concat(left.slice(leftIndex))
        .concat(right.slice(rightIndex));
      this.data.arrNum = results;
    },
  }
};

Sample Original Array Before sorted Output After sorted

about 4 years ago · Juan Pablo Isaza
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