0

I have implemented a quicksort algoritm in javascript. that looks like this

        function quickSort(array){
          if (array.length <= 1) return array;
            var less = [];
            var more = [];
            var equal = [];
            var i;
            var pivot = (array.length / 2) | 0;
            for(i = 0; i < array.length; i++) {
              if (array[i] > array[pivot]) {
                more.push(array[i]);
            } else if (array[i] < array[pivot]) {
                less.push(array[i]);
              } else {
                equal.push(array[i]);
              }
            }
            return quickSort(less).concat(equal, quickSort(more));
        }
        document.write(quickSort([3,46,78,90,48,32,13,6,45,87,32,56,45])); 

But I want instead of pushing the values in array to different arrays to have it move the values within the array itself. How do I do that?

  • Possible duplicate of [Move an array element from one array position to another](http://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another) – Liam Jul 06 '16 at 10:10
  • In general it is better not to mutate global state (`arr`). If you mutate `arr`, your `quickSort` would perform a side effect. Side effects lead to a stronger coupling and poor reusability. –  Jul 06 '16 at 10:18
  • I think it's ok to mutate `arr`. Classical `quickSort` is "in place", I will expect that it will not require additional O(n) memory to sort an array (until worst case). – Arnial Jul 06 '16 at 10:26

0 Answers0