簡単な並べ替えと泡の並べ替え

3436 ワード

1.簡単な並べ替え
<script type="text/javascript">
    window.οnlοad=function(){
        function compare(value1,value2){
                return  value1 > value2 ? 1:-1;
        };//      
        var arr = ['34','43','55','23','12'];
        alert(arr.sort(compare).toString());
        //12,23,34,43,55
    }
script>
2.発泡体の並べ替え
<script type="text/javascript">
//              
    window.onload = function(){
        var arr=[12,2,5,8,0,68,15,24,22,21,9];
        var len = arr.length;
        //          ,    ,    ,          , 1    2    ,      2     3    ,    。                      
        function bubbleSort(arr){
            var t=0;
            for(var i = 0 ; ifor(var j=0 ; j < len-1 ; j++){
                        if(arr[j]>arr[j+1]){
                            t=arr[j];
                            arr[j]=arr[j+1];
                            arr[j+1] = t;
                        }
                    }
                }
            return arr;
        }
    console.log(bubbleSort(arr));//      
}
script>