javascript - How to write quick sort on Node.js -


i'm continue trying write alrorithms on node.js on book algorithms, 4th ed. sedgewick, wayne. there examples written on java.

i have quick sort module:

"use strict";  const _ = require('lodash');  module.exports = (function () {    function _partition(array, lo, hi) {     let = lo;     let j = hi + 1;     let v = array[lo];      while (true) {       while (_less(array[++i], v)) {         if (i === hi) {           break;         }       }       while (_less(v, array[--j])) {         if (j === lo) {           break;         }       }       if (i >= j) {         break;       }       _exch(array, i, j);     }     _exch(array, lo, j);     return j;   }    function sort(array) {     _sort(array, 0, array.length - 1);   }    function _sort(array, lo, hi) {     if (hi <= lo) {       return null;     }      let j = _partition(array, lo, hi);      _sort(array, lo, j - 1);     _sort(array, j + 1, hi);   }    function _less(array, i, min) {     return array[i] < array[min];   }    function _exch(array, i, min) {     let temp = array[i];     array[i] = array[min];     array[min] = temp;   }    return {     sort: sort   };  })(); 

i use mocha , chai testing function:

function issorted(array) {   for(let = 1, size = array.length; < size; i++) {     if (array[i] < array[i-1]) {       return false;     }   }   return true; } 

and quick sort not working. need same implementation in book, on js.

you can see original implementation here: quick sort in java

your function _less uses indexes arguments:

  function _less(array, i, min) {     return array[i] < array[min]; 

but call array elements values:

 let v = array[lo];  while (_less(array[++i], v) 

(and omit first argument array - legal in js?)


Comments