码迷,mamicode.com
首页 > 其他好文 > 详细

Bubble Sort

时间:2016-06-07 22:01:29      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
    </head>
    <body>

<script type="text/javascript">
    Array.prototype.bubble_sort = function(){
        var i, j, temp;
        for (i = 0; i < this.length - 1; i++) {
            for (j = 0; j < this.length - 1 - i; j++) {
                if(this[j] > this[j + 1]){
                    temp = this[j];
                    this[j] = this[j + 1];
                    this[j + 1] = temp;
                }
            };
        };
        return this;
    }

    Array.prototype.bubble_sort2 = function(){
        var i, j, temp;
        for (i = 0; i < this.length - 1; i++) {
            for (j = 0; j < this.length - 1 - i; j++) {
                if(this[j] < this[j + 1]){
                    temp = this[j];
                    this[j] = this[j + 1];
                    this[j + 1] = temp;
                }
            };
        };
        return this;
    }

    var num = [22, 34, 3, 32, 82, 55, 89, 50, 37, 5, 64, 35, 9, 70];
    num.bubble_sort();
    for (var i = 0; i < num.length; i++)
    document.body.innerHTML += num[i] + " ";

    document.body.innerHTML += "<br>";

    num.bubble_sort2();
    for (var i = 0; i < num.length; i++)
    document.body.innerHTML += num[i] + " ";

</script>

    </body>
</html>

  

Bubble Sort

标签:

原文地址:http://www.cnblogs.com/yzzz/p/5568297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!