Mélange de tableaux avec l'algorithme de Fisher-Yates

JavaScriptJavaScriptBeginner
Pratiquer maintenant

This tutorial is from open-source community. Access the source code

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

Dans ce laboratoire, nous explorerons l'algorithme de Fisher-Yates et son implantation en JavaScript. Plus précisément, nous nous concentrerons sur le mélange d'éléments dans un tableau à l'aide de cet algorithme. À la fin de ce laboratoire, vous aurez une meilleure compréhension de fonctionnement de l'algorithme de Fisher-Yates et de la manière dont il peut être utilisé pour aléatoriser l'ordre des éléments dans un tableau.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("JavaScript")) -.-> javascript/BasicConceptsGroup(["Basic Concepts"]) javascript(("JavaScript")) -.-> javascript/AdvancedConceptsGroup(["Advanced Concepts"]) javascript/BasicConceptsGroup -.-> javascript/variables("Variables") javascript/BasicConceptsGroup -.-> javascript/data_types("Data Types") javascript/BasicConceptsGroup -.-> javascript/arith_ops("Arithmetic Operators") javascript/BasicConceptsGroup -.-> javascript/comp_ops("Comparison Operators") javascript/BasicConceptsGroup -.-> javascript/loops("Loops") javascript/BasicConceptsGroup -.-> javascript/array_methods("Array Methods") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("Spread and Rest Operators") subgraph Lab Skills javascript/variables -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/data_types -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/arith_ops -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/comp_ops -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/loops -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/array_methods -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} javascript/spread_rest -.-> lab-28615{{"Mélange de tableaux avec l'algorithme de Fisher-Yates"}} end

Algorithme de mélange de tableau

Pour mélanger un tableau en JavaScript, utilisez l'algorithme de Fisher-Yates. Cet algorithme réordonne aléatoirement les éléments du tableau et renvoie un nouveau tableau.

Pour commencer à pratiquer la programmation, ouvrez le Terminal/SSH et tapez node.

Voici le code pour l'algorithme de Fisher-Yates :

const shuffle = ([...arr]) => {
  let m = arr.length;
  while (m) {
    const i = Math.floor(Math.random() * m--);
    [arr[m], arr[i]] = [arr[i], arr[m]];
  }
  return arr;
};

Pour mélanger un tableau, passez-le à la fonction shuffle et elle renverra le tableau mélangé. Par exemple :

const foo = [1, 2, 3];
shuffle(foo); // renvoie [2, 3, 1], et foo est toujours [1, 2, 3]

Sommaire

Félicitations ! Vous avez terminé le laboratoire sur le mélange de tableaux. Vous pouvez pratiquer d'autres laboratoires sur LabEx pour améliorer vos compétences.