首頁 >web前端 >js教程 >Javascript實作的常用演算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧

Javascript實作的常用演算法(如冒泡、快速、鴿巢、奇偶等)_javascript技巧

WBOY
WBOY原創
2016-05-16 16:50:52960瀏覽
複製程式碼 程式碼如下:

l = [6, 2, 4, 1, 98, 34, 5, 9, 23, 8, 10, 32]

     /*function bubbleSort(arr) {
    newarr = arr.slice()
    if (newarr.length i ) {
        for (var j = 0; j          = [newarr[j 1], newarr[j 1] = newarr[j]][0]

            }}

console.log(l)
result = bubbleSort(l)
console.log(result)*/

       
//快速排序    if (arr.length     var left = [],
      arr[ Math.floor(arr.length / 2)]
    for (var i = 0; i         arr[i]);
        } else if (arr[i] > mid) {
                     middle.push(mid);
        }
    }
    return [].concat(quickSort(左), m, QuickSort(右))

> 🎜>console.log(結果)

       
*/

//選擇排序
/*function selectSort(arr) {        minindex = 0;
    var left = [],
        right = arr.slice();        if (arr[i]            >        }
    }
    left.push(分鐘);   
    right.splice(minindex, 1);
    return [].concat(left, selectSort(right))var results = selectSort(l)
console.log(結果)*/

       
//插入排序
/*function insertSort
//插入排序
/*function insertSort(arr(arr);對於(var i = 1; i
        tmp = arr[i];
        for (var j = i; j > 0; j--) {           } else {
                  }
        arr[j] = tmp;
    }
    返回arr;
}

var results = insertSort(l)
console.log(results)*/

//木桶排序
/*function bucketSort(arr){    var bucket = [];
    var newarr = [];
    for (var i = 0; i 桶🎜> 桶[arri] ];
    }
    for (var i = 0; i         if (bucket[i] !== undefine 🎜> }
    }
    return newarr;
}

var results = bucketSort(l)
console.log(results)>/食肉>負非整數
/*函數pigingholeSort(arr){
    var tempArr = [];
    for(var i=0,l=arr.length;i temp i=0,l=arr.length;i    }

    var result = [],count;
           count = tempArr[k];
        if(count){
                        result.push ( k);
            }
        }     
var results = PigeonSort(l)
console.log(結果) */

//歸併排序
/*function mergeSort(arr) {
    if (arr.length   if (arr.length 長度/ 2);
    var left = MergeSort(arr.slice(0, mid));
    var right = MergeSort(arr.slice(mid));
    var result = [];

    while (left.length && right.length) {
        if (left[left ] ] .length - 1]             結果= result.concat(left);
    else if (right[right.length - 1] < ; 左[0]) {
                       if (右[0]                result.push(right.shift());
         push(left.shift());
            }
        }
    回傳結果;
}*/

       

       

       

       

       

= function(left, right) {
        var result = [];
        whior 左[0]                result.push(left.shift());
         result.push(右.轉變());
            }
        }    }

    if (arr.length == 1) return arr;
    var middle = Math.floor(arr.length / 2),
        left = arr.slice(0, middle),}

var results = mergeSort(l)
console.log(results)

*/

    *function heapSort(arr) {
    var findRoot = function(arr, p, length) {
        p = p || 0;
        長度 = 長度 || arr.length;
        var self = argument.callee;
        var l = p * 2 1
        if (l         if (r         if (右> arr[p]) arr[p] = [右, arr[r ] = arr[ p]][0];

        return arr[p];
    };

  . {
        findRoot(arr, 0, i);
        arr[i - 1] = [arr[0], arr[0] = arr[i - 11][0];
    }
    return arr;
}

var results = heapSort(l)
console.log(值)*/ // 奇偶排列

/*function oddEvenSort(arr) {
    var swaped = true,
         if (k > 0) ) swaped = false;

        for (var i = k; i       >                arr[i] = [ arr[i 1], arr[i 1]=arr[i] ][0];
           }
        }
        k = [1 , 0][k]
    }
    return arr;
}

var results = oddEvenSort(l)
con🎜>var results = oddEvenSort(l)
console. function oddEvenSort(arr) {
    var swaped = true;
    while (swaped) {
     .長度; i = 2) {
            if (arr[i] > arr[i 1]) {
            交換= true;
            }
        }
          if (到達[i]> ; arr[i 1]) {
                arr[i] = [arr[i 1], arr[i 1] = arr[i]][0];        }
    }
    return arr;
}

var results = oddEvenSort(l)
console.log(結果)

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn