Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. var a = ['S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E'];
  2.  
  3. function qSort(from, to) {
  4. if (from >= to) {
  5. return;
  6. }
  7. var pivot = a[to];
  8. var counter = from;
  9.  
  10. for (var i = from; i < to; i++) {
  11. if (a[i] < pivot) {
  12. qSwap(i, counter);
  13. counter++;
  14. }
  15. }
  16. qSwap(counter, to);
  17. qSort(from, counter - 1);
  18. qSort(counter + 1, to);
  19. }
  20.  
  21. function qSwap(x, y) {
  22. let temp = a[x];
  23. a[x] = a[y];
  24. a[y] = temp;
  25. }
  26.  
  27. qSort(0, a.length - 1);
  28. console.log(a);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement