Skip to content

Tiah7/bubble-sort-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Bubble-sort-js (tri à bulles)

C'est un exercice en javascript pour créer un tri à bulles à partir d'un algorithme de tri ou "bubble Sort" :

Dans main.js :

                    const chart = document.getElementById('chart');

                    console.log(chart);
                    let array = [];
                    for(let i = 0; i<100; i++){
                        let number = Math.floor(Math.random()*100);
                        array.push(number);
                    }
                        function draw() {
                            chart.innerHTML = '';
                            array.forEach(function (x) {
                                let div = document.createElement('div');
                                div.style.height = x+'px';
                                chart.appendChild(div);
                            })
                        }
                        //draw ();

                        async function bubbleSort() {
                            for (let i = 0; i < array.length; i++) {
                                for (let j = 0; j < array.length; j++) {
                                    if (array[j] > array[j+1]) {
                                        let temp = array[j];
                                        array[j] = array[j+1];
                                        array[j+1] = temp;    
                                    }
                                    draw();
                                    await sleep(1);
                                }
                            }
                            function sleep(ms) {
                                return new Promise(resolve => setTimeout(resolve,ms));
                            }
                        }
                        bubbleSort();

About

Les exercices en javascript

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published