打赏

javascript 冒泡排序算法

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>javascript 冒泡排序算法</title>

    </head>

    <body>
        <script type="text/javascript">
            function bubbleArr(arr) {
                var i, j, len = arr.length;
                for(i = 0; i < len - 1; i++) {
                    for(j = 0; j < len - 1 - i; j++) {
                        if(arr[j] > arr[j + 1]) {
                            var temp = arr[j];
                            arr[j] = arr[j + 1];
                            arr[j + 1] = temp;
                        }
                    }
                }
            }
            var arr = [11, 2, 34, 34, 2, 546, 4];
            bubbleArr(arr);
            console.log(arr)
        </script>
    </body>

</html>

 

posted @ 2018-04-02 12:06  孟繁贵  阅读(181)  评论(0编辑  收藏  举报
TOP