JavaScript實現(xiàn)快速排序的方法
更新時間:2015年07月31日 11:25:32 作者:華宰
這篇文章主要介紹了JavaScript實現(xiàn)快速排序的方法,實例分析了javascript快速排序的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JavaScript實現(xiàn)快速排序的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
<html>
<head>
<script>
function quickSort(input) {
if (input.length <= 1) return input;
var pivot = Math.floor(Math.random()*input.length)
var less = [], greater=[];
var pivotElem = input.splice(pivot,1)
for (x in input) {
if (input[x] <= pivotElem[0])
less.push(input[x])
else
greater.push(input[x])
}
return [].concat(quickSort(less),pivotElem,quickSort(greater));
}
input = []
inputSize = 1000
highestInputValue = 100
for (i=0;i<inputSize;i++) {
input.push(Math.floor(Math.random()*highestInputValue))
}
document.writeln(quickSort(input))
</script>
</head>
</body>
</html>
希望本文所述對大家的javascript程序設(shè)計有所幫助。
相關(guān)文章
JavaScript canvas實現(xiàn)刮刮樂案例
這篇文章主要為大家詳細介紹了JavaScript canvas實現(xiàn)刮刮樂案例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10

