xonecas.io
2620
Ja

js-algos / commit e167300

added insertion sort

xonecas2013-04-26 e167300af0203f93fc6686776b71d654975d1b15 parent 2bf7707

11 files changed

Makefile+3−2
@@ -11,8 +11,8 @@ docs:
1111
1212 module:
1313 @echo "----> Creating Module..."
14- uglifyjs -c -m --wrap 'algos' src/js/*.js > algos.min.js
15- uglifyjs -b --wrap 'algos' src/js/*.js > algos.js
14+ uglifyjs src/js/*.js -c -m --wrap algos > algos.min.js
15+ uglifyjs src/js/*.js -b --wrap algos > algos.js
1616
1717 clean:
1818 rm -rf docs algos*
@@ -21,5 +21,6 @@ test: lint module
2121 @echo "----> Testing..."
2222 node test/quicksort.js
2323 node test/mergesort.js
24+ node test/insertionsort.js
2425
2526 .PHONY: docs module
README.md+6−0
@@ -3,6 +3,12 @@ Algorithms study.
33
44 I'm brushing up on these things.
55
6+So I figured I would make a JS library with them.
7+
8+Use `make` to build it all. It depends on uglifyjs jslint and docco cli commands in your path.
9+
10+To run tests use `make test`
11+
612 License
713 =======
814
algos.js+29−2
@@ -1,6 +1,27 @@
11 (function(exports, global) {
22 global["algos"] = exports;
33 "use strict";
4+ var insertionSort;
5+ insertionSort = function(list) {
6+ var len = list.length, val, i, hole;
7+ for (i = 1; i < len; i += 1) {
8+ val = list[i];
9+ hole = i;
10+ while (hole > 0 && val < list[hole - 1]) {
11+ list[hole] = list[hole - 1];
12+ hole -= 1;
13+ }
14+ list[hole] = val;
15+ }
16+ return list;
17+ };
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";
425 var join, mergeSort;
526 join = function(a, b) {
627 var joined = [], ai = 0, bi = 0;
@@ -22,8 +43,11 @@
2243 var m = Math.floor(list.length / 2), a = list.slice(0, m), b = list.slice(m);
2344 return join(mergeSort(a), mergeSort(b));
2445 };
25- if (module) {
46+ if (module && module.exports) {
2647 module.exports.mergeSort = mergeSort;
48+ } else {
49+ window.algos = window.algos || {};
50+ window.algos.mergeSort = mergeSort;
2751 }
2852 "use strict";
2953 var swap, partition, quickSort;
@@ -64,8 +88,11 @@
6488 }
6589 return list;
6690 };
67- if (module) {
91+ if (module && module.exports) {
6892 module.exports.quickSort = quickSort;
93+ } else {
94+ window.algos = window.algos || {};
95+ window.algos.quickSort = quickSort;
6996 }
7097 })({}, function() {
7198 return this;
algos.min.js+1−1
@@ -1 +1 @@
1-(function(r,t){"use strict";t.algos=r;var n,o;n=function(r,t){for(var n=[],o=0,e=0;r.length>o&&t.length>e;)r[o]<t[e]?(n.push(r[o]),o+=1):(n.push(t[e]),e+=1);return n.concat(r.slice(o).concat(t.slice(e)))},o=function(r){if(1>=r.length)return r;var t=Math.floor(r.length/2),e=r.slice(0,t),u=r.slice(t);return n(o(e),o(u))},module&&(module.exports.mergeSort=o);var e,u,c;e=function(r,t,n){var o=r[t];r[t]=r[n],r[n]=o},u=function(r,t,n){for(var o=r[Math.floor((t+n)/2)];n>=t;){for(;o>r[t];)t+=1;for(;r[n] …
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. …
docs/insertionsort.html+44−0
@@ -0,0 +1,44 @@
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+10−6
@@ -1,7 +1,6 @@
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="mergesort.html"> mergesort.js …
2-
3-<span class="kd">var</span> <span class="nx">join</span><span class="p">,</span>
4- <span class="nx">mergeSort</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>Join two sorted arrays together.</p>
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>
54
65 <p>Create two pointers, one for each list, point them both to the value
76 at index 0. Keep comparing the idexes values and keep the lowest in the
@@ -21,7 +20,9 @@ result. Ta-Da!</p> </td> <td class="code">
2120 <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>
2221 <span class="nx">bi</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
2322 <span class="p">}</span>
24- <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>fancy... joining the last remaining items..</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">joined</span><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 …
2526
2627 <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>
2728
@@ -42,8 +43,11 @@ as we pile them back up in order one by one.</p> </td> <
4243
4344 <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>
4445
45-<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>expose as the global/module (part of the build process with uglifyjs</p> </td> <td class="code"> <div class="highlight"><pre><span class="k">if</span> <span class="p">(</sp …
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; …
4647 <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>
4751 <span class="p">}</span>
4852
4953 </pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/quicksort.html+10−8
@@ -1,11 +1,10 @@
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="mergesort.html"> mergesort.js …
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>
23
34 <p>Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
4-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="s1">&#39;use strict&#39;</span><span class="p">;</span>
5-
6-<span class="kd">var</span> <span class="nx">swap</span><span class="p">,</span>
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>
76 <span class="nx">partition</span><span class="p">,</span>
8- <span class="nx">quickSort</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>easy peasy swap function.</p>
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>
98
109 <p>think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
1110 we want the beer in the blue glass and the wine in the red one.
@@ -15,7 +14,7 @@ pour the beer into the blue one.</p> </td> <td class="co
1514 <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>
1615 <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>
1716 <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>
18-<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>creates a smaller partition of the array</p>
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>
1918
2019 <p>starting with a middle point because if the array is sorted you'll get
2120 the worst possible performance if you choose the first or last elements.
@@ -46,7 +45,7 @@ return the left pointer so it can be used as the starting point for the next par
4645 <span class="p">}</span>
4746
4847 <span class="k">return</span> <span class="nx">left</span><span class="p">;</span>
49-<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>Only operate on lists with 2 or more values.
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.
5049 use default values for left and right if they are undefined (first run).
5150 partition the array.
5251 if the left pointer can still be moved right towards the index, call quicksort again in that direction.
@@ -70,8 +69,11 @@ if the right pointer can be moved left, call quicksort from the current index.</
7069 <span class="p">}</span>
7170
7271 <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
73-<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>can this be used for sorting lists of strings alphabetically ?</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> …
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; …
7473 <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>
7577 <span class="p">}</span>
7678
7779 </pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
src/js/insertionsort.js+52−0
@@ -0,0 +1,52 @@
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+}
src/js/mergesort.js+7−3
@@ -1,5 +1,6 @@
1-// Let's try merge sort, seems cool.
1+/*jslint node:true browser:true */
22 'use strict';
3+// Let's try merge sort, seems cool.
34
45 var join,
56 mergeSort;
@@ -57,7 +58,10 @@ mergeSort = function (list) {
5758
5859 };
5960
60-// expose as the global/module (part of the build process with uglifyjs
61-if (module) {
61+// module jargon
62+if (module && module.exports) {
6263 module.exports.mergeSort = mergeSort;
64+} else {
65+ window.algos = window.algos || {};
66+ window.algos.mergeSort = mergeSort;
6367 }
src/js/quicksort.js+7−5
@@ -1,8 +1,9 @@
1+/*jslint node:true browser:true */
2+'use strict';
13 // quick sort baby!
24 //
35 // Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
46 // much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
5-'use strict';
67
78 var swap,
89 partition,
@@ -89,9 +90,10 @@ quickSort = function (list, left, right) {
8990 };
9091
9192
92-// can this be used for sorting lists of strings alphabetically ?
93-
94-// expose as the global/module (part of the build process with uglifyjs
95-if (module) {
93+// module jargon
94+if (module && module.exports) {
9695 module.exports.quickSort = quickSort;
96+} else {
97+ window.algos = window.algos || {};
98+ window.algos.quickSort = quickSort;
9799 }
test/insertionsort.js+7−0
@@ -0,0 +1,7 @@
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));