Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function quicksort(array, left, right) {
   left = left || 0;
   right = right || array.length - 1;
  
   const pivot = partitionHoare(array, left, right);
   
   if (left < pivot - 1) {
      quicksort(array, left, pivot - 1);
   }
   if (right > pivot) {
      quicksort(array, pivot, right);
   }
   return array;
}

function partitionHoare(array, left, right) {
   const pivot = Math.floor(Math.random() * (right - left + 1) + left);
   while (left <= right) {
     while (array[left] < array[pivot]) {  
        left++;
     }  
     while (array[right] > array[pivot]) {
       right--;
     }
     if (left <= right) {
       [array[left], array[right]] = [array[right], array[left]];
     }
   }
   return left;
}

partitionHoare([9, 5, 3, 1, 2], 9, 2)
node v10.15.2 linux/amd64