首页 > 其他 > 详细

Bubble Sort

时间:2016-06-07 21:59:04      阅读:175      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!