Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. import { generateRandomList, swap } from '../helpers';
  2. import { Logger } from '../logger';
  3.  
  4. const partition = (list: number[], low: number, high: number): number => {
  5. const pivot: number = list[high];
  6. let i: number = low;
  7.  
  8. for (let j = low; j <= high - 1; j++) {
  9. if (list[j] < pivot) {
  10. swap(list, i, j);
  11. i++;
  12. }
  13. }
  14.  
  15. swap(list, i, high);
  16.  
  17. return i;
  18. };
  19.  
  20. const quicksort = (list: number[], low: number, high: number) => {
  21. if (list.length <= 1) {
  22. return;
  23. }
  24.  
  25. if (low < high) {
  26. const partitionIndex = partition(list, low, high);
  27. quicksort(list, low, partitionIndex - 1);
  28. quicksort(list, partitionIndex + 1, high);
  29. }
  30. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement