這篇文章主要介紹了JavaScript實現快速排序的方法,實例分析了javascript快速排序的相關實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了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程序設計有所幫助。
新聞熱點
疑難解答
圖片精選