Risto-Stevcev/javascript-quicksort

View on GitHub
doc/quicksortjs.js.html

Summary

Maintainability
Test Coverage
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: quicksortjs.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: quicksortjs.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>/**
 * @module quicksortjs
 * @author Risto Stevcev
 */


/**
 * @method
 * @param {Array} array An array to sort
 * @returns {Array} The sorted array
 */
exports.sort = function sort(array) {
  if (array.length === 0) 
    return [];
  var x = array.pop();
  var smallerSorted = sort(array.filter(function(e) { return e &lt;= x; }));
  var biggerSorted  = sort(array.filter(function(e) { return e > x; }));
  return Array.prototype.concat(smallerSorted, [x], biggerSorted);
};


/**
 * @method
 * @param {Array} array An array to sort
 * @returns {Array} The sorted array
 */
exports.sort2 = function sort2(list) {
  function __sort2__(list, left, right) {
    var swap = function(list, a, b) {
      temp = list[a];
      list[a] = list[b];
      list[b] = temp;
    };

    var partition = function(list, left, right, pivotidx) {
      pivot = list[pivotidx];
      swap(list, right, pivotidx);

      for (i = left; i &lt; right; i++) {
        if (list[i] &lt; pivot) {
          swap(list, i, left);
          left++;
        }
      }
      swap(list, left, right);
      return left;
    };

    if (right > left) {
      pivotidx = Math.floor(Math.random() * (right - left + 1)) + left;
      pivotidx = partition(list, left, right, pivotidx);
      __sort2__(list, left, pivotidx);
      __sort2__(list, pivotidx+1, right);
    }
    return list;
  }
  return __sort2__(list, 0, list.length - 1);
}
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-quicksortjs.html">quicksortjs</a></li></ul>
</nav>

<br clear="both">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-alpha11</a> on Mon Dec 08 2014 17:44:21 GMT-0800 (PST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>