xonecas.io
2620
Ja

js-algos / commit cae2d18

playing around with build, bubble sort and shuffle added

xonecas2013-05-01 cae2d18c9d87d026250a3a37265de02b9a6bb3e6 parent a5642cc

17 files changed

This diff is too big to show in full. The first 1000 lines are below, and every file is listed with its counts.

Makefile+9−9
@@ -2,25 +2,25 @@ all: lint docs module
22
33 lint:
44 @echo "----> Linting JS..."
5- jslint --browser true --node true --white true {src/js,test}/*.js
6- uglifyjs --lint {src/js,test}/*.js > /dev/null
5+ jslint --browser true --node true --white true js/algos.js
6+ uglifyjs --lint js/algos.js > /dev/null
7+ jslint --browser true --node true --white true test/*.js
8+ uglifyjs --lint test/*.js > /dev/null
79
810 docs:
911 @echo "----> Making docs..."
10- docco -c css/docco.css src/js/*.js
12+ docco -c css/docco.css js/algos.js
1113
1214 module:
1315 @echo "----> Creating Module..."
14- uglifyjs src/js/*.js -c -m --wrap algos > algos.min.js
15- uglifyjs src/js/*.js -b --wrap algos > algos.js
16+ cat js/{head,algos,tail}.js | uglifyjs -c -m > algos.min.js
17+ cat js/{head,algos,tail}.js | uglifyjs -b > algos.js
1618
1719 clean:
18- rm -rf docs algos*
20+ rm -rf docs algos.min.js
1921
2022 test: lint module
2123 @echo "----> Testing..."
22- node test/quicksort.js
23- node test/mergesort.js
24- node test/insertionsort.js
24+ node test/sorts.js
2525
2626 .PHONY: lint docs module clean test
algos.js+31−26
@@ -1,8 +1,8 @@
1-(function(exports, global) {
2- global["algos"] = exports;
1+(function() {
32 "use strict";
3+ var algos = {};
44 var insertionSort;
5- insertionSort = function(list) {
5+ insertionSort = algos.insertionSort = function(list) {
66 var len = list.length, val, i, hole;
77 for (i = 1; i < len; i += 1) {
88 val = list[i];
@@ -15,13 +15,6 @@
1515 }
1616 return list;
1717 };
18- if (module && module.exports) {
19- module.exports.insertionSort = insertionSort;
20- } else {
21- window.algos = window.algos || {};
22- window.algos.insertionSort = insertionSort;
23- }
24- "use strict";
2518 var join, mergeSort;
2619 join = function(a, b) {
2720 var joined = [], ai = 0, bi = 0;
@@ -36,20 +29,13 @@
3629 }
3730 return joined.concat(a.slice(ai).concat(b.slice(bi)));
3831 };
39- mergeSort = function(list) {
32+ mergeSort = algos.mergeSort = function(list) {
4033 if (list.length <= 1) {
4134 return list;
4235 }
4336 var m = Math.floor(list.length / 2), a = list.slice(0, m), b = list.slice(m);
4437 return join(mergeSort(a), mergeSort(b));
4538 };
46- if (module && module.exports) {
47- module.exports.mergeSort = mergeSort;
48- } else {
49- window.algos = window.algos || {};
50- window.algos.mergeSort = mergeSort;
51- }
52- "use strict";
5339 var swap, partition, quickSort;
5440 swap = function(list, first, second) {
5541 var tmp = list[first];
@@ -73,7 +59,7 @@
7359 }
7460 return left;
7561 };
76- quickSort = function(list, left, right) {
62+ quickSort = algos.quickSort = function(list, left, right) {
7763 var idx;
7864 if (list.length >= 2) {
7965 left = left === undefined ? 0 : left;
@@ -88,12 +74,31 @@
8874 }
8975 return list;
9076 };
91- if (module && module.exports) {
92- module.exports.quickSort = quickSort;
77+ var bubbleSort;
78+ bubbleSort = algos.bubbleSort = function(list) {
79+ var len = list.length, i, j;
80+ for (i = 0; i < len; i += 1) {
81+ for (j = 0; j < len - i; j += 1) {
82+ if (list[j] > list[j + 1]) {
83+ swap(list, j, j + 1);
84+ }
85+ }
86+ }
87+ return list;
88+ };
89+ var shuffle;
90+ shuffle = algos.shuffle = function(list) {
91+ var len = list.length, max = len - 1;
92+ while (len) {
93+ len -= 1;
94+ swap(list, len, Math.floor(Math.random() * max));
95+ }
96+ return list;
97+ };
98+ if (module && module.exports || exports) {
99+ module = module || {};
100+ exports = module.exports = algos;
93101 } else {
94- window.algos = window.algos || {};
95- window.algos.quickSort = quickSort;
102+ window.algos = algos;
96103 }
97-})({}, function() {
98- return this;
99-}());
104+})();
algos.min.js+1−1
@@ -1 +1 @@
1-(function(o,r){"use strict";r.algos=o;var n;n=function(o){var r,n,t,e=o.length;for(n=1;e>n;n+=1){for(r=o[n],t=n;t>0&&o[t-1]>r;)o[t]=o[t-1],t-=1;o[t]=r}return o},module&&module.exports?module.exports.insertionSort=n:(window.algos=window.algos||{},window.algos.insertionSort=n);var t,e;t=function(o,r){for(var n=[],t=0,e=0;o.length>t&&r.length>e;)o[t]<r[e]?(n.push(o[t]),t+=1):(n.push(r[e]),e+=1);return n.concat(o.slice(t).concat(r.slice(e)))},e=function(o){if(1>=o.length)return o;var r=Math.floor(o. …
1+(function(){"use strict";var r,o={};r=o.insertionSort=function(r){var o,t,n,e=r.length;for(t=1;e>t;t+=1){for(o=r[t],n=t;n>0&&r[n-1]>o;)r[n]=r[n-1],n-=1;r[n]=o}return r};var t,n;t=function(r,o){for(var t=[],n=0,e=0;r.length>n&&o.length>e;)r[n]<o[e]?(t.push(r[n]),n+=1):(t.push(o[e]),e+=1);return t.concat(r.slice(n).concat(o.slice(e)))},n=o.mergeSort=function(r){if(1>=r.length)return r;var o=Math.floor(r.length/2),e=r.slice(0,o),u=r.slice(o);return t(n(e),n(u))};var e,u,f;e=function(r,o,t){var n=r[ …
docs/algos.html+180−0
@@ -0,0 +1,180 @@
1+<!DOCTYPE html> <html> <head> <title>algos.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> algos.js </h1> </th> <th class="code"> </th> </tr> </th …
2+<span class="s1">&#39;use strict&#39;</span><span class="p">;</span>
3+<span class="kd">var</span> <span class="nx">algos</span> <span class="o">=</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <h1>Insertion sort, the stable sort.</h1>
4+
5+<p>resources:
6+https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
7+http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">insertionSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> …
8+each one had a marble with a value written on it. To order the values on the balls we start
9+with the second cup from the left and take the ball out. Now compare the value of the loose
10+ball with the value of the ball in the cup to the left of the empty cup. If the value is
11+greater then we grab that ball and move it to the now empty cup. Continue to do this while the
12+values of the balls in the cups to the left are greater than the loose balls vlaue. Once you
13+reach the last cup on the left or a value that is lesser than the loose ball's value, just
14+place the loose ball in the empty cup and move right to the next cup.
15+By the time you reach the last cup on the right, you'll have sorted the balls per their values
16+in ascending order.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">insertionSort</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">insertionSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
17+ <span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span>
18+ <span class="nx">val</span><span class="p">,</span>
19+ <span class="nx">i</span><span class="p">,</span>
20+ <span class="nx">hole</span><span class="p">;</span>
21+
22+ <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
23+
24+ <span class="nx">val</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
25+ <span class="nx">hole</span> <span class="o">=</span> <span class="nx">i</span><span class="p">;</span>
26+
27+ <span class="k">while</span> <span class="p">(</span><span class="nx">hole</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span> <span class="o">&lt;</span> <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="p">{</span>
28+ <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span><span class="p">]</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
29+ <span class="nx">hole</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
30+ <span class="p">}</span>
31+
32+ <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
33+
34+ <span class="p">}</span>
35+
36+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
37+
38+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <h1>Let's try merge sort, seems cool.</h1> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">join</span><span class="p">,</span>
39+ <span class="nx">mergeSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Join two sorted arrays together.</p>
40+
41+<p>Create two pointers, one for each list, point them both to the value
42+at index 0. Keep comparing the idexes values and keep the lowest in the
43+joined list, increment by 1 the respective index. Do this until one of
44+the indexes matches the length of it's list.
45+Simple pile up the remainder of the orther list at the end of the joined
46+result. Ta-Da!</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">join</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
47+ <span class="kd">var</span> <span class="nx">joined</span> <span class="o">=</span> <span class="p">[],</span>
48+ <span class="nx">ai</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
49+ <span class="nx">bi</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
50+
51+ <span class="k">while</span> <span class="p">(</span><span class="nx">ai</span> <span class="o">&lt;</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">&amp;&amp;</span> <span class="nx">bi</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
52+ <span class="k">if</span> <span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">ai</span><span class="p">]</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="p">[</span><span class="nx">bi</span><span class="p">])</span> <span class="p">{</span>
53+ <span class="nx">joined</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">ai</span><span class="p">]);</span>
54+ <span class="nx">ai</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
55+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
56+ <span class="nx">joined</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">b</span><span class="p">[</span><span class="nx">bi</span><span class="p">]);</span>
57+ <span class="nx">bi</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
58+ <span class="p">}</span>
59+ <span class="p">}</span>
60+
61+ <span class="k">return</span> <span class="nx">joined</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">ai</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">bi</span><span class="p">)));</s …
62+
63+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Now for the cool part</p>
64+
65+<p>Don't you love recursion? Base case is a single item list, that needs no
66+sort and can be returned as is. The recursive step finds a middle to the
67+list and cuts it in half.
68+We return the recursive call, so that we keep on splitting smaller and
69+smaller lists until their are of a single item, and then we return them
70+as we pile them back up in order one by one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">mergeSort</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">mergeSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
71+
72+ <span class="k">if</span> <span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
73+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
74+ <span class="p">}</span>
75+
76+ <span class="kd">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span>
77+ <span class="nx">a</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">m</span><span class="p">),</span>
78+ <span class="nx">b</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">m</span><span class="p">);</span>
79+
80+ <span class="k">return</span> <span class="nx">join</span><span class="p">(</span><span class="nx">mergeSort</span><span class="p">(</span><span class="nx">a</span><span class="p">),</span> <span class="nx">mergeSort</span><span class="p">(</span><span class="nx">b</span><span class="p">));</span>
81+
82+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <h1>quick sort baby!</h1>
83+
84+<p>Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
85+much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">swap</span><span class="p">,</span>
86+ <span class="nx">partition</span><span class="p">,</span>
87+ <span class="nx">quickSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>easy peasy swap function.</p>
88+
89+<p>think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
90+we want the beer in the blue glass and the wine in the red one.
91+pour the beer in the white one,
92+pour the wine in the red one,
93+pour the beer into the blue one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">swap</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span><span class="p">)</span> <span class="p">{</span>
94+ <span class="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">first</span><span class="p">];</span>
95+ <span class="nx">list</span><span class="p">[</span><span class="nx">first</span><span class="p">]</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">second</span><span class="p">];</span>
96+ <span class="nx">list</span><span class="p">[</span><span class="nx">second</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">;</span>
97+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>creates a smaller partition of the array</p>
98+
99+<p>starting with a middle point because if the array is sorted you'll get
100+the worst possible performance if you choose the first or last elements.
101+while the left pointer is smaller or equal to the right pointer
102+move the left pointer to the right while it's value is less than the middle's value.
103+after, move the right pointer to the left while it's value is greater than the middle's value.
104+if the left pointer is smaller or equal to the right pointer swap the pointers values.
105+move the pointers one more time in each direction so that we continue in the correct direction.
106+return the left pointer so it can be used as the starting point for the next partition.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">partition</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
107+ <span class="kd">var</span> <span class="nx">pivot</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span> <span class="p">(</span><span class="nx">left</span> <span class="o">+</span> <span class="nx">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="p">)];</span>
108+
109+ <span class="k">while</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;=</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
110+
111+ <span class="k">while</span> <span class="p">(</span><span class="nx">list</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="nx">pivot</span><span class="p">)</span> <span class="p">{</span>
112+ <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
113+ <span class="p">}</span>
114+
115+ <span class="k">while</span> <span class="p">(</span><span class="nx">list</span><span class="p">[</span><span class="nx">right</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nx">pivot</span><span class="p">)</span> <span class="p">{</span>
116+ <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
117+ <span class="p">}</span>
118+
119+ <span class="k">if</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;=</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
120+ <span class="nx">swap</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
121+ <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
122+ <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
123+ <span class="p">}</span>
124+
125+ <span class="p">}</span>
126+
127+ <span class="k">return</span> <span class="nx">left</span><span class="p">;</span>
128+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Only operate on lists with 2 or more values.
129+use default values for left and right if they are undefined (first run).
130+partition the array.
131+if the left pointer can still be moved right towards the index, call quicksort again in that direction.
132+if the right pointer can be moved left, call quicksort from the current index.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">quickSort</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">quickSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,< …
133+ <span class="kd">var</span> <span class="nx">idx</span><span class="p">;</span>
134+
135+ <span class="k">if</span> <span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
136+
137+ <span class="nx">left</span> <span class="o">=</span> <span class="nx">left</span> <span class="o">===</span> <span class="kc">undefined</span> <span class="o">?</span> <span class="mi">0</span> <span class="o">:</span> <span class="nx">left</span><span class="p">;</span>
138+ <span class="nx">right</span> <span class="o">=</span> <span class="nx">right</span> <span class="o">===</span> <span class="kc">undefined</span> <span class="o">?</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">right</span><span class="p">;</span>
139+
140+ <span class="nx">idx</span> <span class="o">=</span> <span class="nx">partition</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
141+
142+ <span class="k">if</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;</span> <span class="nx">idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
143+ <span class="nx">quickSort</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
144+ <span class="p">}</span>
145+
146+ <span class="k">if</span> <span class="p">(</span><span class="nx">idx</span> <span class="o">&lt;</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
147+ <span class="nx">quickSort</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">idx</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
148+ <span class="p">}</span>
149+ <span class="p">}</span>
150+
151+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
152+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <h1>Bubble sort, aka blubbly</h1> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">bubbleSort</span><span class="p">;</span>< …
153+higher values to the top. VERY SLOW! n^2 slow...</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">bubbleSort</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">bubbleSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
154+ <span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span>
155+ <span class="nx">i</span><span class="p">,</span>
156+ <span class="nx">j</span><span class="p">;</span>
157+
158+ <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
159+ <span class="k">for</span> <span class="p">(</span><span class="nx">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">j</span> <span class="o">&lt;</span> <span class="p">(</span><span class="nx">len</span> <span class="o">-</span> <span class="nx">i</span><span class="p">);</span> <span class="nx">j</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
160+ <span class="k">if</span> <span class="p">(</span><span class="nx">list</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nx">list</span><span class="p">[</span><span class="nx">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span> <span class="p">{</span>
161+ <span class="nx">swap</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">j</span><span class="p">,</span> <span class="nx">j</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
162+ <span class="p">}</span>
163+ <span class="p">}</span>
164+ <span class="p">}</span>
165+
166+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
167+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <h1>shuffle, make a mess of things...</h1> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">shuffle</span><span class="p">;</ …
168+for each item in the array, create a random number within the length and swap.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">shuffle</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">shuffle</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
169+ <span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span>
170+ <span class="nx">max</span> <span class="o">=</span> <span class="nx">len</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span>
171+
172+ <span class="k">while</span> <span class="p">(</span><span class="nx">len</span><span class="p">)</span> <span class="p">{</span>
173+ <span class="nx">len</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
174+ <span class="nx">swap</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">len</span><span class="p">,</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span> <span class="o">*</span> <span class="nx">max</span> <span class="p">));</span>
175+ <span class="p">}</span>
176+
177+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
178+<span class="p">};</span>
179+
180+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/insertionsort.html+0−44
@@ -1,44 +0,0 @@
1-<!DOCTYPE html> <html> <head> <title>insertionsort.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="insertionsort.html"> insertionsort.js …
2-<span class="s1">&#39;use strict&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Insertion sort, the stable sort.</p>
3-
4-<p>resources:
5-https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
6-http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">insertionSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> …
7-each one had a marble with a value written on it. To order the values on the balls we start
8-with the second cup from the left and take the ball out. Now compare the value of the loose
9-ball with the value of the ball in the cup to the left of the empty cup. If the value is
10-greater then we grab that ball and move it to the now empty cup. Continue to do this while the
11-values of the balls in the cups to the left are greater than the loose balls vlaue. Once you
12-reach the last cup on the left or a value that is lesser than the loose ball's value, just
13-place the loose ball in the empty cup and move right to the next cup.
14-By the time you reach the last cup on the right, you'll have sorted the balls per their values
15-in ascending order.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">insertionSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
16- <span class="kd">var</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span><span class="p">,</span>
17- <span class="nx">val</span><span class="p">,</span>
18- <span class="nx">i</span><span class="p">,</span>
19- <span class="nx">hole</span><span class="p">;</span>
20-
21- <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="nx">i</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
22-
23- <span class="nx">val</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
24- <span class="nx">hole</span> <span class="o">=</span> <span class="nx">i</span><span class="p">;</span>
25-
26- <span class="k">while</span> <span class="p">(</span><span class="nx">hole</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">val</span> <span class="o">&lt;</span> <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span> <span class="o">-</span> <span class="mi">1</span><span class="p">])</span> <span class="p">{</span>
27- <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span><span class="p">]</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span> <span class="o">-</span> <span class="mi">1</span><span class="p">];</span>
28- <span class="nx">hole</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
29- <span class="p">}</span>
30-
31- <span class="nx">list</span><span class="p">[</span><span class="nx">hole</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
32-
33- <span class="p">}</span>
34-
35- <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
36-
37-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>module jargon</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="nx">module</span> <span class="o">&amp; …
38- <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">insertionSort</span> <span class="o">=</span> <span class="nx">insertionSort</span><span class="p">;</span>
39-<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
40- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">||</span> <span class="p">{};</span>
41- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span><span class="p">.</span><span class="nx">insertionSort</span> <span class="o">=</span> <span class="nx">insertionSort</span><span class="p">;</span>
42-<span class="p">}</span>
43-
44-</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/mergesort.html+0−53
@@ -1,53 +0,0 @@
1-<!DOCTYPE html> <html> <head> <title>mergesort.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="insertionsort.html"> insertionsort.js …
2-<span class="s1">&#39;use strict&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Let's try merge sort, seems cool.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class …
3- <span class="nx">mergeSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>Join two sorted arrays together.</p>
4-
5-<p>Create two pointers, one for each list, point them both to the value
6-at index 0. Keep comparing the idexes values and keep the lowest in the
7-joined list, increment by 1 the respective index. Do this until one of
8-the indexes matches the length of it's list.
9-Simple pile up the remainder of the orther list at the end of the joined
10-result. Ta-Da!</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">join</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="p">{</span>
11- <span class="kd">var</span> <span class="nx">joined</span> <span class="o">=</span> <span class="p">[],</span>
12- <span class="nx">ai</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
13- <span class="nx">bi</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
14-
15- <span class="k">while</span> <span class="p">(</span><span class="nx">ai</span> <span class="o">&lt;</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">&amp;&amp;</span> <span class="nx">bi</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
16- <span class="k">if</span> <span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">ai</span><span class="p">]</span> <span class="o">&lt;</span> <span class="nx">b</span><span class="p">[</span><span class="nx">bi</span><span class="p">])</span> <span class="p">{</span>
17- <span class="nx">joined</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="nx">ai</span><span class="p">]);</span>
18- <span class="nx">ai</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
19- <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
20- <span class="nx">joined</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">b</span><span class="p">[</span><span class="nx">bi</span><span class="p">]);</span>
21- <span class="nx">bi</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
22- <span class="p">}</span>
23- <span class="p">}</span>
24-
25- <span class="k">return</span> <span class="nx">joined</span><span class="p">.</span><span class="nx">concat</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">ai</span><span class="p">).</span><span class="nx">concat</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">bi</span><span class="p">)));</s …
26-
27-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Now for the cool part</p>
28-
29-<p>Don't you love recursion? Base case is a single item list, that needs no
30-sort and can be returned as is. The recursive step finds a middle to the
31-list and cuts it in half.
32-We return the recursive call, so that we keep on splitting smaller and
33-smaller lists until their are of a single item, and then we return them
34-as we pile them back up in order one by one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">mergeSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">)</span> <span class="p">{</span>
35-
36- <span class="k">if</span> <span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
37- <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
38- <span class="p">}</span>
39-
40- <span class="kd">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span>
41- <span class="nx">a</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">m</span><span class="p">),</span>
42- <span class="nx">b</span> <span class="o">=</span> <span class="nx">list</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">m</span><span class="p">);</span>
43-
44- <span class="k">return</span> <span class="nx">join</span><span class="p">(</span><span class="nx">mergeSort</span><span class="p">(</span><span class="nx">a</span><span class="p">),</span> <span class="nx">mergeSort</span><span class="p">(</span><span class="nx">b</span><span class="p">));</span>
45-
46-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>module jargon</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="nx">module</span> <span class="o">&amp; …
47- <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">mergeSort</span> <span class="o">=</span> <span class="nx">mergeSort</span><span class="p">;</span>
48-<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
49- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">||</span> <span class="p">{};</span>
50- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span><span class="p">.</span><span class="nx">mergeSort</span> <span class="o">=</span> <span class="nx">mergeSort</span><span class="p">;</span>
51-<span class="p">}</span>
52-
53-</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/quicksort.html+0−79
@@ -1,79 +0,0 @@
1-<!DOCTYPE html> <html> <head> <title>quicksort.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="insertionsort.html"> insertionsort.js …
2-<span class="s1">&#39;use strict&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>quick sort baby! </p>
3-
4-<p>Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
5-much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">swap</span><span class="p">,</span>
6- <span class="nx">partition</span><span class="p">,</span>
7- <span class="nx">quickSort</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>easy peasy swap function.</p>
8-
9-<p>think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
10-we want the beer in the blue glass and the wine in the red one.
11-pour the beer in the white one,
12-pour the wine in the red one,
13-pour the beer into the blue one.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">swap</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">first</span><span class="p">,</span> <span class="nx">second</span><span class="p">)</span> <span class="p">{</span>
14- <span class="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">first</span><span class="p">];</span>
15- <span class="nx">list</span><span class="p">[</span><span class="nx">first</span><span class="p">]</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nx">second</span><span class="p">];</span>
16- <span class="nx">list</span><span class="p">[</span><span class="nx">second</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">;</span>
17-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>creates a smaller partition of the array</p>
18-
19-<p>starting with a middle point because if the array is sorted you'll get
20-the worst possible performance if you choose the first or last elements.
21-while the left pointer is smaller or equal to the right pointer
22-move the left pointer to the right while it's value is less than the middle's value.
23-after, move the right pointer to the left while it's value is greater than the middle's value.
24-if the left pointer is smaller or equal to the right pointer swap the pointers values.
25-move the pointers one more time in each direction so that we continue in the correct direction.
26-return the left pointer so it can be used as the starting point for the next partition.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">partition</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
27- <span class="kd">var</span> <span class="nx">pivot</span> <span class="o">=</span> <span class="nx">list</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span> <span class="p">(</span><span class="nx">left</span> <span class="o">+</span> <span class="nx">right</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="p">)];</span>
28-
29- <span class="k">while</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;=</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
30-
31- <span class="k">while</span> <span class="p">(</span><span class="nx">list</span><span class="p">[</span><span class="nx">left</span><span class="p">]</span> <span class="o">&lt;</span> <span class="nx">pivot</span><span class="p">)</span> <span class="p">{</span>
32- <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
33- <span class="p">}</span>
34-
35- <span class="k">while</span> <span class="p">(</span><span class="nx">list</span><span class="p">[</span><span class="nx">right</span><span class="p">]</span> <span class="o">&gt;</span> <span class="nx">pivot</span><span class="p">)</span> <span class="p">{</span>
36- <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
37- <span class="p">}</span>
38-
39- <span class="k">if</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;=</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
40- <span class="nx">swap</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
41- <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
42- <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
43- <span class="p">}</span>
44-
45- <span class="p">}</span>
46-
47- <span class="k">return</span> <span class="nx">left</span><span class="p">;</span>
48-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Only operate on lists with 2 or more values.
49-use default values for left and right if they are undefined (first run).
50-partition the array.
51-if the left pointer can still be moved right towards the index, call quicksort again in that direction.
52-if the right pointer can be moved left, call quicksort from the current index.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">quickSort</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
53- <span class="kd">var</span> <span class="nx">idx</span><span class="p">;</span>
54-
55- <span class="k">if</span> <span class="p">(</span><span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">)</span> <span class="p">{</span>
56-
57- <span class="nx">left</span> <span class="o">=</span> <span class="nx">left</span> <span class="o">===</span> <span class="kc">undefined</span> <span class="o">?</span> <span class="mi">0</span> <span class="o">:</span> <span class="nx">left</span><span class="p">;</span>
58- <span class="nx">right</span> <span class="o">=</span> <span class="nx">right</span> <span class="o">===</span> <span class="kc">undefined</span> <span class="o">?</span> <span class="nx">list</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">:</span> <span class="nx">right</span><span class="p">;</span>
59-
60- <span class="nx">idx</span> <span class="o">=</span> <span class="nx">partition</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
61-
62- <span class="k">if</span> <span class="p">(</span><span class="nx">left</span> <span class="o">&lt;</span> <span class="nx">idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
63- <span class="nx">quickSort</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">left</span><span class="p">,</span> <span class="nx">idx</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
64- <span class="p">}</span>
65-
66- <span class="k">if</span> <span class="p">(</span><span class="nx">idx</span> <span class="o">&lt;</span> <span class="nx">right</span><span class="p">)</span> <span class="p">{</span>
67- <span class="nx">quickSort</span><span class="p">(</span><span class="nx">list</span><span class="p">,</span> <span class="nx">idx</span><span class="p">,</span> <span class="nx">right</span><span class="p">);</span>
68- <span class="p">}</span>
69- <span class="p">}</span>
70-
71- <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
72-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>module jargon</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="p">(</span><span class="nx">module</span> <span class="o">&amp; …
73- <span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">quickSort</span> <span class="o">=</span> <span class="nx">quickSort</span><span class="p">;</span>
74-<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
75- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span> <span class="o">||</span> <span class="p">{};</span>
76- <span class="nb">window</span><span class="p">.</span><span class="nx">algos</span><span class="p">.</span><span class="nx">quickSort</span> <span class="o">=</span> <span class="nx">quickSort</span><span class="p">;</span>
77-<span class="p">}</span>
78-
79-</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
js/algos.js+248−0
@@ -0,0 +1,248 @@
1+/*jslint node:true browser:true */
2+'use strict';
3+var algos = {};
4+// Insertion sort, the stable sort.
5+// ================================
6+//
7+// resources:
8+// https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
9+// http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/
10+
11+var insertionSort;
12+
13+// Ok, so what on earth is this? it's quite simple to explain visually. Think of bunch of cups,
14+// each one had a marble with a value written on it. To order the values on the balls we start
15+// with the second cup from the left and take the ball out. Now compare the value of the loose
16+// ball with the value of the ball in the cup to the left of the empty cup. If the value is
17+// greater then we grab that ball and move it to the now empty cup. Continue to do this while the
18+// values of the balls in the cups to the left are greater than the loose balls vlaue. Once you
19+// reach the last cup on the left or a value that is lesser than the loose ball's value, just
20+// place the loose ball in the empty cup and move right to the next cup.
21+// By the time you reach the last cup on the right, you'll have sorted the balls per their values
22+// in ascending order.
23+
24+insertionSort = algos.insertionSort = function (list) {
25+ var len = list.length,
26+ val,
27+ i,
28+ hole;
29+
30+ for (i = 1; i < len; i += 1) {
31+
32+ val = list[i];
33+ hole = i;
34+
35+ while (hole > 0 && val < list[hole - 1]) {
36+ list[hole] = list[hole - 1];
37+ hole -= 1;
38+ }
39+
40+ list[hole] = val;
41+
42+ }
43+
44+ return list;
45+
46+};
47+
48+
49+
50+// Let's try merge sort, seems cool.
51+// ================================
52+
53+var join,
54+ mergeSort;
55+
56+
57+// Join two sorted arrays together.
58+//
59+// Create two pointers, one for each list, point them both to the value
60+// at index 0. Keep comparing the idexes values and keep the lowest in the
61+// joined list, increment by 1 the respective index. Do this until one of
62+// the indexes matches the length of it's list.
63+// Simple pile up the remainder of the orther list at the end of the joined
64+// result. Ta-Da!
65+
66+join = function (a, b) {
67+ var joined = [],
68+ ai = 0,
69+ bi = 0;
70+
71+ while (ai < a.length && bi < b.length) {
72+ if (a[ai] < b[bi]) {
73+ joined.push(a[ai]);
74+ ai += 1;
75+ } else {
76+ joined.push(b[bi]);
77+ bi += 1;
78+ }
79+ }
80+
81+ return joined.concat(a.slice(ai).concat(b.slice(bi)));
82+
83+};
84+
85+
86+// Now for the cool part
87+//
88+// Don't you love recursion? Base case is a single item list, that needs no
89+// sort and can be returned as is. The recursive step finds a middle to the
90+// list and cuts it in half.
91+// We return the recursive call, so that we keep on splitting smaller and
92+// smaller lists until their are of a single item, and then we return them
93+// as we pile them back up in order one by one.
94+
95+mergeSort = algos.mergeSort = function (list) {
96+
97+ if (list.length <= 1) {
98+ return list;
99+ }
100+
101+ var m = Math.floor(list.length / 2),
102+ a = list.slice(0, m),
103+ b = list.slice(m);
104+
105+ return join(mergeSort(a), mergeSort(b));
106+
107+};
108+
109+
110+
111+// quick sort baby!
112+// ================
113+//
114+// Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
115+// much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
116+
117+var swap,
118+ partition,
119+ quickSort;
120+
121+
122+// easy peasy swap function.
123+//
124+// think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
125+// we want the beer in the blue glass and the wine in the red one.
126+// pour the beer in the white one,
127+// pour the wine in the red one,
128+// pour the beer into the blue one.
129+
130+swap = function (list, first, second) {
131+ var tmp = list[first];
132+ list[first] = list[second];
133+ list[second] = tmp;
134+};
135+
136+
137+// creates a smaller partition of the array
138+//
139+// starting with a middle point because if the array is sorted you'll get
140+// the worst possible performance if you choose the first or last elements.
141+// while the left pointer is smaller or equal to the right pointer
142+// move the left pointer to the right while it's value is less than the middle's value.
143+// after, move the right pointer to the left while it's value is greater than the middle's value.
144+// if the left pointer is smaller or equal to the right pointer swap the pointers values.
145+// move the pointers one more time in each direction so that we continue in the correct direction.
146+// return the left pointer so it can be used as the starting point for the next partition.
147+
148+partition = function (list, left, right) {
149+ var pivot = list[Math.floor( (left + right) / 2 )];
150+
151+ while (left <= right) {
152+
153+ while (list[left] < pivot) {
154+ left += 1;
155+ }
156+
157+ while (list[right] > pivot) {
158+ right -= 1;
159+ }
160+
161+ if (left <= right) {
162+ swap(list, left, right);
163+ left += 1;
164+ right -= 1;
165+ }
166+
167+ }
168+
169+ return left;
170+};
171+
172+
173+// Only operate on lists with 2 or more values.
174+// use default values for left and right if they are undefined (first run).
175+// partition the array.
176+// if the left pointer can still be moved right towards the index, call quicksort again in that direction.
177+// if the right pointer can be moved left, call quicksort from the current index.
178+
179+quickSort = algos.quickSort = function (list, left, right) {
180+ var idx;
181+
182+ if (list.length >= 2) {
183+
184+ left = left === undefined ? 0 : left;
185+ right = right === undefined ? list.length - 1 : right;
186+
187+ idx = partition(list, left, right);
188+
189+ if (left < idx - 1) {
190+ quickSort(list, left, idx - 1);
191+ }
192+
193+ if (idx < right) {
194+ quickSort(list, idx, right);
195+ }
196+ }
197+
198+ return list;
199+};
200+
201+
202+
203+// Bubble sort, aka blubbly
204+// ========================
205+
206+var bubbleSort;
207+
208+
209+// As the name says, bubbly over here compares all the values in sequence, "bubbling" the
210+// higher values to the top. VERY SLOW! n^2 slow...
211+
212+bubbleSort = algos.bubbleSort = function (list) {
213+ var len = list.length,
214+ i,
215+ j;
216+
217+ for (i = 0; i < len; i += 1) {
218+ for (j = 0; j < (len - i); j += 1) {
219+ if (list[j] > list[j + 1]) {
220+ swap(list, j, j + 1);
221+ }
222+ }
223+ }
224+
225+ return list;
226+};
227+
228+
229+// shuffle, make a mess of things...
230+// =================================
231+
232+var shuffle;
233+
234+
235+// slow and compreensive? fast and simple?
236+// for each item in the array, create a random number within the length and swap.
237+
238+shuffle = algos.shuffle = function (list) {
239+ var len = list.length,
240+ max = len - 1;
241+
242+ while (len) {
243+ len -= 1;
244+ swap(list, len, Math.floor( Math.random() * max ));
245+ }
246+
247+ return list;
248+};
js/head.js+1−0
@@ -0,0 +1 @@
1+(function () {
js/insertionsort.js+0−52
@@ -1,52 +0,0 @@
1-/*jslint node:true browser:true */
2-'use strict';
3-// Insertion sort, the stable sort.
4-//
5-// resources:
6-// https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
7-// http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/
8-
9-var insertionSort;
10-
11-// Ok, so what on earth is this? it's quite simple to explain visually. Think of bunch of cups,
12-// each one had a marble with a value written on it. To order the values on the balls we start
13-// with the second cup from the left and take the ball out. Now compare the value of the loose
14-// ball with the value of the ball in the cup to the left of the empty cup. If the value is
15-// greater then we grab that ball and move it to the now empty cup. Continue to do this while the
16-// values of the balls in the cups to the left are greater than the loose balls vlaue. Once you
17-// reach the last cup on the left or a value that is lesser than the loose ball's value, just
18-// place the loose ball in the empty cup and move right to the next cup.
19-// By the time you reach the last cup on the right, you'll have sorted the balls per their values
20-// in ascending order.
21-
22-insertionSort = function (list) {
23- var len = list.length,
24- val,
25- i,
26- hole;
27-
28- for (i = 1; i < len; i += 1) {
29-
30- val = list[i];
31- hole = i;
32-
33- while (hole > 0 && val < list[hole - 1]) {
34- list[hole] = list[hole - 1];
35- hole -= 1;
36- }
37-
38- list[hole] = val;
39-
40- }
41-
42- return list;
43-
44-};
45-
46-// module jargon
47-if (module && module.exports) {
48- module.exports.insertionSort = insertionSort;
49-} else {
50- window.algos = window.algos || {};
51- window.algos.insertionSort = insertionSort;
52-}
js/mergesort.js+0−67
@@ -1,67 +0,0 @@
1-/*jslint node:true browser:true */
2-'use strict';
3-// Let's try merge sort, seems cool.
4-
5-var join,
6- mergeSort;
7-
8-
9-// Join two sorted arrays together.
10-//
11-// Create two pointers, one for each list, point them both to the value
12-// at index 0. Keep comparing the idexes values and keep the lowest in the
13-// joined list, increment by 1 the respective index. Do this until one of
14-// the indexes matches the length of it's list.
15-// Simple pile up the remainder of the orther list at the end of the joined
16-// result. Ta-Da!
17-
18-join = function (a, b) {
19- var joined = [],
20- ai = 0,
21- bi = 0;
22-
23- while (ai < a.length && bi < b.length) {
24- if (a[ai] < b[bi]) {
25- joined.push(a[ai]);
26- ai += 1;
27- } else {
28- joined.push(b[bi]);
29- bi += 1;
30- }
31- }
32-
33- return joined.concat(a.slice(ai).concat(b.slice(bi)));
34-
35-};
36-
37-
38-// Now for the cool part
39-//
40-// Don't you love recursion? Base case is a single item list, that needs no
41-// sort and can be returned as is. The recursive step finds a middle to the
42-// list and cuts it in half.
43-// We return the recursive call, so that we keep on splitting smaller and
44-// smaller lists until their are of a single item, and then we return them
45-// as we pile them back up in order one by one.
46-
47-mergeSort = function (list) {
48-
49- if (list.length <= 1) {
50- return list;
51- }
52-
53- var m = Math.floor(list.length / 2),
54- a = list.slice(0, m),
55- b = list.slice(m);
56-
57- return join(mergeSort(a), mergeSort(b));
58-
59-};
60-
61-// module jargon
62-if (module && module.exports) {
63- module.exports.mergeSort = mergeSort;
64-} else {
65- window.algos = window.algos || {};
66- window.algos.mergeSort = mergeSort;
67-}
js/quicksort.js+0−99
@@ -1,99 +0,0 @@
1-/*jslint node:true browser:true */
2-'use strict';
3-// quick sort baby!
4-//
5-// Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
6-// much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
7-
8-var swap,
9- partition,
10- quickSort;
11-
12-
13-// easy peasy swap function.
14-//
15-// think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
16-// we want the beer in the blue glass and the wine in the red one.
17-// pour the beer in the white one,
18-// pour the wine in the red one,
19-// pour the beer into the blue one.
20-
21-swap = function (list, first, second) {
22- var tmp = list[first];
23- list[first] = list[second];
24- list[second] = tmp;
25-};
26-
27-
28-// creates a smaller partition of the array
29-//
30-// starting with a middle point because if the array is sorted you'll get
31-// the worst possible performance if you choose the first or last elements.
32-// while the left pointer is smaller or equal to the right pointer
33-// move the left pointer to the right while it's value is less than the middle's value.
34-// after, move the right pointer to the left while it's value is greater than the middle's value.
35-// if the left pointer is smaller or equal to the right pointer swap the pointers values.
36-// move the pointers one more time in each direction so that we continue in the correct direction.
37-// return the left pointer so it can be used as the starting point for the next partition.
38-
39-partition = function (list, left, right) {
40- var pivot = list[Math.floor( (left + right) / 2 )];
41-
42- while (left <= right) {
43-
44- while (list[left] < pivot) {
45- left += 1;
46- }
47-
48- while (list[right] > pivot) {
49- right -= 1;
50- }
51-
52- if (left <= right) {
53- swap(list, left, right);
54- left += 1;
55- right -= 1;
56- }
57-
58- }
59-
60- return left;
61-};
62-
63-
64-// Only operate on lists with 2 or more values.
65-// use default values for left and right if they are undefined (first run).
66-// partition the array.
67-// if the left pointer can still be moved right towards the index, call quicksort again in that direction.
68-// if the right pointer can be moved left, call quicksort from the current index.
69-
70-quickSort = function (list, left, right) {
71- var idx;
72-
73- if (list.length >= 2) {
74-
75- left = left === undefined ? 0 : left;
76- right = right === undefined ? list.length - 1 : right;
77-
78- idx = partition(list, left, right);
79-
80- if (left < idx - 1) {
81- quickSort(list, left, idx - 1);
82- }
83-
84- if (idx < right) {
85- quickSort(list, idx, right);
86- }
87- }
88-
89- return list;
90-};
91-
92-
93-// module jargon
94-if (module && module.exports) {
95- module.exports.quickSort = quickSort;
96-} else {
97- window.algos = window.algos || {};
98- window.algos.quickSort = quickSort;
99-}
js/tail.js+7−0
@@ -0,0 +1,7 @@
1+ if ((module && module.exports) || exports) {
2+ module = module || {};
3+ exports = module.exports = algos;
4+ } else {
5+ window.algos = algos;
6+ }
7+}());
test/insertionsort.js+0−7
@@ -1,7 +0,0 @@
1-'use strict';
2-var insertionSort = require('../algos.js').insertionSort,
3- items = [ 4, 8, 1, 4, 9, 0, 3, 2, 6 ];
4-console.log("JS Insertionsort\nBefore:");
5-console.log(items);
6-console.log("After:");
7-console.log(insertionSort(items));
test/mergesort.js+0−7
@@ -1,7 +0,0 @@
1-'use strict';
2-var mergeSort = require('../algos.js').mergeSort,
3- items = [4, 8, 1, 4, 9, 0, 3, 2, 6];
4-console.log("JS Mergesort\nBefore:");
5-console.log(items);
6-console.log("After:");
7-console.log(mergeSort(items));
test/quicksort.js+0−7
@@ -1,7 +0,0 @@
1-'use strict';
2-var quickSort = require('../algos.js').quickSort,
3- items = [ 4, 8, 1, 4, 9, 0, 3, 2, 6 ];
4-console.log("JS quicksort\nBefore:");
5-console.log(items);
6-console.log("After:");
7-console.log(quickSort(items));
test/sorts.js+29−0
@@ -0,0 +1,29 @@
1+'use strict';
Cut short: this commit's diff is over 1000 lines.