Posted By

naren1012 on 04/06/11


Tagged

javascript quicksort


Versions (?)

Quicksort implementation in javascript


 / Published in: JavaScript
 

This is implementation of pseudocode given in quicksort wikipedia page. Did it just for fun :)

  1. <html>
  2. <head>
  3. <script>
  4. function quickSort(input) {
  5. if (input.length <= 1) return input;
  6. var pivot = Math.floor(Math.random()*input.length)
  7. var less = [], greater=[];
  8. var pivotElem = input.splice(pivot,1)
  9. for (x in input) {
  10. if (input[x] <= pivotElem[0])
  11. less.push(input[x])
  12. else
  13. greater.push(input[x])
  14. }
  15. return [].concat(quickSort(less),pivotElem,quickSort(greater));
  16. }
  17.  
  18. input = []
  19. inputSize = 1000
  20. highestInputValue = 100
  21. for (i=0;i<inputSize;i++) {
  22. input.push(Math.floor(Math.random()*highestInputValue))
  23. }
  24. document.writeln(quickSort(input))
  25. </script>
  26. </head>
  27.  
  28. </body>
  29. </html>

Report this snippet  

You need to login to post a comment.