Selection sort

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8">

    </head>

    <body>

<script type="text/javascript">

    Array.prototype.selection_sort = function(){

        var i, j, min, temp;

        for (i = 0; i < this.length - 1; i++) {    
            min = i;
            for (j = i + 1; j < this.length; j++) {
                if(this[min] > this[j]){
                    min = j;
                }
            temp = this[min];
            this[min] = this[i];
            this[i] = temp;
            };
        };

        return this;

    }

    var num = [7, 3, 2, 100, 5, 4, 0];

    num.selection_sort();

    for (var i = 0; i < num.length; i++)

    document.body.innerHTML += num[i] + " ";
 

</script>


    </body>

</html>

 

posted @ 2016-06-26 22:58  z.yz  阅读(139)  评论(0)    收藏  举报