JavaScript实现快速排序的方法_Javascript教程-查字典教程网
JavaScript实现快速排序的方法
JavaScript实现快速排序的方法
发布时间:2016-12-30 来源:查字典编辑
摘要:本文实例讲述了JavaScript实现快速排序的方法。分享给大家供大家参考。具体实现方法如下:functionquickSort(input...

本文实例讲述了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>

希望本文所述对大家的javascript程序设计有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类