JavaScript實(shí)現(xiàn)快速排序的方法
更新時(shí)間:2015年07月31日 11:25:32 作者:華宰
這篇文章主要介紹了JavaScript實(shí)現(xiàn)快速排序的方法,實(shí)例分析了javascript快速排序的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了JavaScript實(shí)現(xiàn)快速排序的方法。分享給大家供大家參考。具體實(shí)現(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>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- JavaScript sort數(shù)組排序方法和自我實(shí)現(xiàn)排序方法小結(jié)
- 基于JS實(shí)現(xiàn)數(shù)字+字母+中文的混合排序方法
- 幾種經(jīng)典排序算法的JS實(shí)現(xiàn)方法
- JS實(shí)現(xiàn)為排序好的字符串找出重復(fù)行的方法
- JavaScript對(duì)象數(shù)組的排序處理方法
- JavaScript實(shí)現(xiàn)下拉列表框數(shù)據(jù)增加、刪除、上下排序的方法
- js實(shí)現(xiàn)漢字排序的方法
- 數(shù)據(jù)結(jié)構(gòu)中的各種排序方法小結(jié)(JS實(shí)現(xiàn))
相關(guān)文章
原生javascript單例模式的應(yīng)用實(shí)例分析
這篇文章主要介紹了原生javascript單例模式的應(yīng)用,結(jié)合實(shí)例形式分析了JavaScript單例模式的基本功能、原理、應(yīng)用及操作注意事項(xiàng),需要的朋友可以參考下2020-02-02JavaScript canvas實(shí)現(xiàn)刮刮樂案例
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas實(shí)現(xiàn)刮刮樂案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10