主页 > 前端 > javascript >
来源:未知 时间:2015-07-31 15:45 作者:xxadmin 阅读:次
[导读] 本文讲解javascript如何实现快速排序方法 htmlheadscriptfunctionquickSort(input){if(input.length=1)returninput;varpivot=Math.floor(Math.random()*input.length)varless=[],greater=[];varpivotElem=input.splice(pivot,1)for(xininput){i...
|
本文讲解javascript如何实现快速排序方法 <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> |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com