排序問題相信你們都比較熟悉了。用js簡單寫了一下幾種經常使用的排序實現。其中使用了es6的一些語法,而且不只限於數字——支持各類類型的數據的排序。那麼直接上代碼:es6
function compare (a, b) { return (a - b) > 0; } // 冒泡排序O(n^2) const bubbleSort = function (arrayData, compareFn = compare) { let len = arrayData.length; for (let i = len - 1; i > 0; i--) { for (let j = 0; j < i; j++) { if (compareFn(arrayData[j], arrayData[j + 1])) { [arrayData[j], arrayData[j + 1]] = [arrayData[j + 1], arrayData[j]]; } } } } // 快速排序 O(n*logn) function getMid (arrayData) { return arrayData[0]; } const quickSort = function (arrayData, compareFn = compare, getMidFn = getMid) { let len = arrayData.length; if (len < 2) { return arrayData; } let mid = getMidFn(arrayData); let smallGroup = []; let largeGroup = []; let midGroup = []; for (let i = 0; i < len; i++) { if (compareFn(mid, arrayData[i])) { smallGroup.push(arrayData[i]); } else if (compareFn(arrayData[i], mid)) { largeGroup.push(arrayData[i]); } else { midGroup.push(arrayData[i]); } } return [...quickSort(smallGroup, compareFn, getMidFn), ...midGroup, ...quickSort(largeGroup, compareFn, getMidFn)]; } // 歸併排序 O(n*logn) const mergeSort = function (arrayData, compareFn = compare) { let merge = function (leftArray, rightArray, compareFn) { let resultArray = []; while (leftArray.length > 0 && rightArray.length > 0) { if (compareFn(leftArray[0], rightArray[0])) { resultArray.push(leftArray.shift()); } else { resultArray.push(rightArray.shift()); } } return resultArray = resultArray.concat(leftArray, rightArray); } if (arrayData.length <= 1) { return arrayData; } let midIndex = parseInt(arrayData.length / 2); let leftArray = arrayData.slice(0, midIndex); let rightArray = arrayData.slice(midIndex); return merge(mergeSort(leftArray, compareFn), mergeSort(rightArray, compareFn), compareFn); } // test codes // let strArray = ['a', 'c', 'b', 'e', 'd'] // strArray = mergeSort(strArray, (a, b) => { // return (a.codePointAt() - b.codePointAt()) > 0; // }); // console.log(strArray); // let strArray = ['a', 'c', 'b', 'e', 'd'] // bubbleSort(strArray, (a, b) => { // return (a.codePointAt() - b.codePointAt()) > 0; // }); // console.log(strArray);
經過自定義campare函數,用戶能夠對任意類型的數據,使用任意的方式進行排序。快速排序容許用戶自定義取中間值的方法,能夠結合實際狀況,更好的提高排序效率。函數