首页 > 代码库 > 冒泡算法

冒泡算法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>冒泡算法</title>
</head>
    <script>
        var bubbleSort = [9,2,3,8,7,5,4,6,1];
        alert(bubbleSort)
        var a;
        for(var i=0;i<bubbleSort.length;i++){
            for(var j=i+1;j<bubbleSort.length;j++){
                if(bubbleSort[i]>bubbleSort[j]){
                    a=bubbleSort[i];
                    bubbleSort[i]=bubbleSort[j];
                    bubbleSort[j]=a;
                }
                // document.write(j);
            }
            // document.write(‘<br>‘);
        }
        alert(bubbleSort)
    </script>
<body>
    
</body>
</html>

 

冒泡算法