jsの配列の並べ替えアルゴリズム

20523 ワード

面接であれ、実際の仕事であれ、配列の順序は私たちが出会うことができるはずです.特に面接の過程では、順序付けは必ず聞かれるものです.下に整理されたいくつかの並べ替えのアルゴリズムです.
/span>html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>title>
head>
<body>
<script>
    var Sort = {}
    Sort.prototype = {
        //   sort    
        systemSort:function(array){
            return array.sort(function(a, b){
                return a - b;
            });
        },

        //     
        bubbleSort:function(array){
            var i = 0, len = array.length,
                    j, d;
            for(; i<len; i++){
                for(j=0; j<len; j++){
                    if(array[i] < array[j]){
                        d = array[j];
                        array[j] = array[i];
                        array[i] = d;
                    }
                }
            }
            return array;
        },

        //     
        quickSort:function(array){
            //var array = [8,4,6,2,7,9,3,5,74,5];
            //var array =[0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7];
            var i = 0;
            var j = array.length - 1;
            var Sort = function(i, j){
                //     
                if(i == j ){ return };
                var key = array[i];
                var tempi = i; //       
                var tempj = j; //       

                while(j > i){
                    // j <    
                    if(array[j] >= key){
                        j--;
                    }else{
                        array[i] = array[j]
                        //i++ ------------>>    
                        while(j > ++i){
                            if(array[i] > key){
                                array[j] = array[i];
                                break;
                            }
                        }
                    }
                }
                //          key      
                if(tempi == i){
                    Sort(++i, tempj);
                    return ;
                }
                //          key
                array[i] = key;
                //   
                Sort(tempi, i);
                Sort(j, tempj);
            }
            Sort(i, j);
            return array;
        },

        //     
        insertSort:function(array){
            // http://baike.baidu.com/image/d57e99942da24e5dd21b7080
            // http://baike.baidu.com/view/396887.htm
            // var array = [0,1,2,44,4,324,5,65,6,6,34,4,5,6,2,43,5,6,62,43,5,1,4,51,56,76,7,7,2,1,45,4,6,7];
            var i = 1, j, temp, key, len = array.length;
            for(; i < len; i++){
                temp = j = i;
                key = array[j];
                while(--j > -1){
                    if(array[j] > key){
                        array[j+1] = array[j];
                    }else{
                        break;
                    }
                }
                array[j+1] = key;
            }
            return array;
        }
    }
    var array = [13,14,94,33,82,25,59,94,65,23,45,27,73,25,39,10];
   console.log(Sort.prototype.systemSort(array).toString())
   console.log(Sort.prototype.bubbleSort(array).toString())
   console.log(Sort.prototype.quickSort(array).toString())
   console.log(Sort.prototype.insertSort(array).toString())
script>
body>
html>