xonecas.io
2620
Ja

js-algos / commit a412ac3

Merge branch 'es6' into 'master'

Es6

See merge request xonecas/js-algos!1

xonecas2017-12-05 a412ac34fbf843bfd37eb32772c885b937ca0107 parent 2fb71e8parent 20fab66

13 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.

.gitignore+2−0
@@ -0,0 +1,2 @@
1+node_modules/
2+docs/
Makefile+0−27
@@ -1,27 +0,0 @@
1-all: lint docs module
2-
3-lint:
4- @echo "----> Linting JS..."
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
9-
10-docs:
11- @echo "----> Making docs..."
12- docco -c css/docco.css js/algos.js
13-
14-module:
15- @echo "----> Creating Module..."
16- cat js/{head,algos,tail}.js | uglifyjs -c -m > algos.min.js
17- cat js/{head,algos,tail}.js | uglifyjs -b > algos.js
18-
19-clean:
20- rm -rf docs algos.min.js
21-
22-test: lint module
23- @echo "----> Testing..."
24- node test/sorts.js
25- node test/levs.js
26-
27-.PHONY: lint docs module clean test
algos.js+0−147
@@ -1,147 +0,0 @@
1-(function() {
2- "use strict";
3- var algos = {};
4- var insertionSort;
5- insertionSort = algos.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- var join, mergeSort;
19- join = function(a, b) {
20- var joined = [], ai = 0, bi = 0;
21- while (ai < a.length && bi < b.length) {
22- if (a[ai] < b[bi]) {
23- joined.push(a[ai]);
24- ai += 1;
25- } else {
26- joined.push(b[bi]);
27- bi += 1;
28- }
29- }
30- return joined.concat(a.slice(ai).concat(b.slice(bi)));
31- };
32- mergeSort = algos.mergeSort = function(list) {
33- if (list.length <= 1) {
34- return list;
35- }
36- var m = Math.floor(list.length / 2), a = list.slice(0, m), b = list.slice(m);
37- return join(mergeSort(a), mergeSort(b));
38- };
39- var swap, partition, quickSort;
40- swap = function(list, first, second) {
41- var tmp = list[first];
42- list[first] = list[second];
43- list[second] = tmp;
44- };
45- partition = function(list, left, right) {
46- var pivot = list[Math.floor((left + right) / 2)];
47- while (left <= right) {
48- while (list[left] < pivot) {
49- left += 1;
50- }
51- while (list[right] > pivot) {
52- right -= 1;
53- }
54- if (left <= right) {
55- swap(list, left, right);
56- left += 1;
57- right -= 1;
58- }
59- }
60- return left;
61- };
62- quickSort = algos.quickSort = function(list, left, right) {
63- var idx;
64- if (list.length >= 2) {
65- left = left === undefined ? 0 : left;
66- right = right === undefined ? list.length - 1 : right;
67- idx = partition(list, left, right);
68- if (left < idx - 1) {
69- quickSort(list, left, idx - 1);
70- }
71- if (idx < right) {
72- quickSort(list, idx, right);
73- }
74- }
75- return list;
76- };
77- var randomizedPartition, randomizedQuickSort;
78- randomizedPartition = function(list, left, right) {
79- var pivot = list[Math.floor(Math.random() * (right - left) + left)];
80- while (left <= right) {
81- while (list[left] < pivot) {
82- left += 1;
83- }
84- while (list[right] > pivot) {
85- right -= 1;
86- }
87- if (left <= right) {
88- swap(list, left, right);
89- left += 1;
90- right -= 1;
91- }
92- }
93- return left;
94- };
95- randomizedQuickSort = algos.randomizedQuickSort = function(list, left, right) {
96- var idx;
97- if (list.length >= 2) {
98- left = left === undefined ? 0 : left;
99- right = right === undefined ? list.length - 1 : right;
100- idx = randomizedPartition(list, left, right);
101- if (left < idx - 1) {
102- randomizedQuickSort(list, left, idx - 1);
103- }
104- if (idx < right) {
105- randomizedQuickSort(list, idx, right);
106- }
107- }
108- return list;
109- };
110- var bubbleSort;
111- bubbleSort = algos.bubbleSort = function(list) {
112- var len = list.length, i, j;
113- for (i = 0; i < len; i += 1) {
114- for (j = 0; j < len - i; j += 1) {
115- if (list[j] > list[j + 1]) {
116- swap(list, j, j + 1);
117- }
118- }
119- }
120- return list;
121- };
122- var shuffle;
123- shuffle = algos.shuffle = function(list) {
124- var len = list.length, max = len - 1;
125- while (len) {
126- len -= 1;
127- swap(list, len, Math.floor(Math.random() * max));
128- }
129- return list;
130- };
131- var fuzzy;
132- fuzzy = algos.fuzzy = function(a, b) {
133- if (a === b) {
134- return 0;
135- }
136- if (!a.length || !b.length) {
137- return 0;
138- }
139- return Math.min(fuzzy(a.substring(1), b) + 1, fuzzy(b.substring(1), a) + 1, fuzzy(a.substring(1), b.substring(1)) + (a[0] !== b[0] ? 1 : 0));
140- };
141- if (module && module.exports || exports) {
142- module = module || {};
143- exports = module.exports = algos;
144- } else {
145- window.algos = algos;
146- }
147-})();
algos.min.js+0−1
@@ -1 +0,0 @@
1-(function(){"use strict";var r,t={};r=t.insertionSort=function(r){var t,n,o,e=r.length;for(n=1;e>n;n+=1){for(t=r[n],o=n;o>0&&r[o-1]>t;)r[o]=r[o-1],o-=1;r[o]=t}return 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=t.mergeSort=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))};var e,u,i;e=function(r,t,n){var o=r[ …
css/docco.css+0−192
@@ -1,192 +0,0 @@
1-/*--------------------- Layout and Typography ----------------------------*/
2-body {
3- font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
4- font-size: 15px;
5- line-height: 22px;
6- color: #252519;
7- margin: 0; padding: 0;
8-}
9-a {
10- color: #261a3b;
11-}
12- a:visited {
13- color: #261a3b;
14- }
15-p {
16- margin: 0 0 15px 0;
17-}
18-h1, h2, h3, h4, h5, h6 {
19- margin: 0px 0 15px 0;
20-}
21- h1 {
22- margin-top: 40px;
23- }
24-hr {
25- border: 0 none;
26- border-top: 1px solid #e5e5ee;
27- height: 1px;
28- margin: 20px 0;
29-}
30-#container {
31- position: relative;
32-}
33-#background {
34- position: fixed;
35- top: 0; left: 525px; right: 0; bottom: 0;
36- background: #f5f5ff;
37- border-left: 1px solid #e5e5ee;
38- z-index: -1;
39-}
40-#jump_to, #jump_page {
41- background: white;
42- -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
43- -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;
44- font: 10px Arial;
45- text-transform: uppercase;
46- cursor: pointer;
47- text-align: right;
48-}
49-#jump_to, #jump_wrapper {
50- position: fixed;
51- right: 0; top: 0;
52- padding: 5px 10px;
53-}
54- #jump_wrapper {
55- padding: 0;
56- display: none;
57- }
58- #jump_to:hover #jump_wrapper {
59- display: block;
60- }
61- #jump_page {
62- padding: 5px 0 3px;
63- margin: 0 0 25px 25px;
64- }
65- #jump_page .source {
66- display: block;
67- padding: 5px 10px;
68- text-decoration: none;
69- border-top: 1px solid #eee;
70- }
71- #jump_page .source:hover {
72- background: #f5f5ff;
73- }
74- #jump_page .source:first-child {
75- }
76-table td {
77- border: 0;
78- outline: 0;
79-}
80- td.docs, th.docs {
81- max-width: 450px;
82- min-width: 450px;
83- min-height: 5px;
84- padding: 10px 25px 1px 50px;
85- overflow-x: hidden;
86- vertical-align: top;
87- text-align: left;
88- }
89- .docs pre {
90- margin: 15px 0 15px;
91- padding-left: 15px;
92- }
93- .docs p tt, .docs p code {
94- background: #f8f8ff;
95- border: 1px solid #dedede;
96- font-size: 12px;
97- padding: 0 0.2em;
98- }
99- .pilwrap {
100- position: relative;
101- }
102- .pilcrow {
103- font: 12px Arial;
104- text-decoration: none;
105- color: #454545;
106- position: absolute;
107- top: 3px; left: -20px;
108- padding: 1px 2px;
109- opacity: 0;
110- -webkit-transition: opacity 0.2s linear;
111- }
112- td.docs:hover .pilcrow {
113- opacity: 1;
114- }
115- td.code, th.code {
116- padding: 14px 15px 16px 25px;
117- width: 100%;
118- vertical-align: top;
119- background: #f5f5ff;
120- border-left: 1px solid #e5e5ee;
121- }
122- pre, tt, code {
123- font-size: 12px; line-height: 18px;
124- font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace;
125- margin: 0; padding: 0;
126- }
127-
128-
129-/*---------------------- Syntax Highlighting -----------------------------*/
130-td.linenos { background-color: #000; padding-right: 10px; }
131-span.lineno { background-color: #000; padding: 0 5px 0 5px; }
132-body .hll { background-color: #ffffcc }
133-body .c { color: #408080; font-style: italic } /* Comment */
134-body .err { border: 1px solid #FF0000 } /* Error */
135-body .k { color: #954121 } /* Keyword */
136-body .o { color: #666666 } /* Operator */
137-body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
138-body .cp { color: #BC7A00 } /* Comment.Preproc */
139-body .c1 { color: #408080; font-style: italic } /* Comment.Single */
140-body .cs { color: #408080; font-style: italic } /* Comment.Special */
141-body .gd { color: #A00000 } /* Generic.Deleted */
142-body .ge { font-style: italic } /* Generic.Emph */
143-body .gr { color: #FF0000 } /* Generic.Error */
144-body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
145-body .gi { color: #00A000 } /* Generic.Inserted */
146-body .go { color: #808080 } /* Generic.Output */
147-body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
148-body .gs { font-weight: bold } /* Generic.Strong */
149-body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
150-body .gt { color: #0040D0 } /* Generic.Traceback */
151-body .kc { color: #954121 } /* Keyword.Constant */
152-body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
153-body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
154-body .kp { color: #954121 } /* Keyword.Pseudo */
155-body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
156-body .kt { color: #B00040 } /* Keyword.Type */
157-body .m { color: #666666 } /* Literal.Number */
158-body .s { color: #219161 } /* Literal.String */
159-body .na { color: #7D9029 } /* Name.Attribute */
160-body .nb { color: #954121 } /* Name.Builtin */
161-body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
162-body .no { color: #880000 } /* Name.Constant */
163-body .nd { color: #AA22FF } /* Name.Decorator */
164-body .ni { color: #999999; font-weight: bold } /* Name.Entity */
165-body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
166-body .nf { color: #0000FF } /* Name.Function */
167-body .nl { color: #A0A000 } /* Name.Label */
168-body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
169-body .nt { color: #954121; font-weight: bold } /* Name.Tag */
170-body .nv { color: #19469D } /* Name.Variable */
171-body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
172-body .w { color: #bbbbbb } /* Text.Whitespace */
173-body .mf { color: #666666 } /* Literal.Number.Float */
174-body .mh { color: #666666 } /* Literal.Number.Hex */
175-body .mi { color: #666666 } /* Literal.Number.Integer */
176-body .mo { color: #666666 } /* Literal.Number.Oct */
177-body .sb { color: #219161 } /* Literal.String.Backtick */
178-body .sc { color: #219161 } /* Literal.String.Char */
179-body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
180-body .s2 { color: #219161 } /* Literal.String.Double */
181-body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
182-body .sh { color: #219161 } /* Literal.String.Heredoc */
183-body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
184-body .sx { color: #954121 } /* Literal.String.Other */
185-body .sr { color: #BB6688 } /* Literal.String.Regex */
186-body .s1 { color: #219161 } /* Literal.String.Single */
187-body .ss { color: #19469D } /* Literal.String.Symbol */
188-body .bp { color: #954121 } /* Name.Builtin.Pseudo */
189-body .vc { color: #19469D } /* Name.Variable.Class */
190-body .vg { color: #19469D } /* Name.Variable.Global */
191-body .vi { color: #19469D } /* Name.Variable.Instance */
192-body .il { color: #666666 } /* Literal.Number.Integer.Long */
docs/algos.html+0−193
@@ -1,193 +0,0 @@
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></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <h1>Levenshtein distance (fuzzy text matching)</h1> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">fuzzy</span><span class= …
179-<code>git</code> and <code>got</code> have a Levenshtein distance of 1, since only one edit needs to be made
180-to tranform them into equal strings. Levenshtein is not very efficient and for performance
181-concerns it should only be applied to small strings at a time, since checking a massive text
182-would take forever(ish).</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">fuzzy</span> <span class="o">=</span> <span class="nx">algos</span><span class="p">.</span><span class="nx">fuzzy</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></pre></div> </td> …
183- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">a</span><span class="p">.</span><span class="nx">length</span> <span class="o">||</span> <span class="o">!</span><span class="nx">b</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span>
184-
185- <span class="k">return</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">(</span>
186- <span class="nx">fuzzy</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="nx">b</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
187- <span class="nx">fuzzy</span><span class="p">(</span><span class="nx">b</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="nx">a</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
188- <span class="nx">fuzzy</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="nx">b</span><span class="p">.</span><span class="nx">substring</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="p">(</span><span class="nx">a</span><span class="p">[</span><span class="mi">0</span><s …
189- <span class="p">);</span>
190-
191-<span class="p">};</span>
192-
193-</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/docco.css+0−192
@@ -1,192 +0,0 @@
1-/*--------------------- Layout and Typography ----------------------------*/
2-body {
3- font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
4- font-size: 15px;
5- line-height: 22px;
6- color: #252519;
7- margin: 0; padding: 0;
8-}
9-a {
10- color: #261a3b;
11-}
12- a:visited {
13- color: #261a3b;
14- }
15-p {
16- margin: 0 0 15px 0;
17-}
18-h1, h2, h3, h4, h5, h6 {
19- margin: 0px 0 15px 0;
20-}
21- h1 {
22- margin-top: 40px;
23- }
24-hr {
25- border: 0 none;
26- border-top: 1px solid #e5e5ee;
27- height: 1px;
28- margin: 20px 0;
29-}
30-#container {
31- position: relative;
32-}
33-#background {
34- position: fixed;
35- top: 0; left: 525px; right: 0; bottom: 0;
36- background: #f5f5ff;
37- border-left: 1px solid #e5e5ee;
38- z-index: -1;
39-}
40-#jump_to, #jump_page {
41- background: white;
42- -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777;
43- -webkit-border-bottom-left-radius: 5px; -moz-border-radius-bottomleft: 5px;
44- font: 10px Arial;
45- text-transform: uppercase;
46- cursor: pointer;
47- text-align: right;
48-}
49-#jump_to, #jump_wrapper {
50- position: fixed;
51- right: 0; top: 0;
52- padding: 5px 10px;
53-}
54- #jump_wrapper {
55- padding: 0;
56- display: none;
57- }
58- #jump_to:hover #jump_wrapper {
59- display: block;
60- }
61- #jump_page {
62- padding: 5px 0 3px;
63- margin: 0 0 25px 25px;
64- }
65- #jump_page .source {
66- display: block;
67- padding: 5px 10px;
68- text-decoration: none;
69- border-top: 1px solid #eee;
70- }
71- #jump_page .source:hover {
72- background: #f5f5ff;
73- }
74- #jump_page .source:first-child {
75- }
76-table td {
77- border: 0;
78- outline: 0;
79-}
80- td.docs, th.docs {
81- max-width: 450px;
82- min-width: 450px;
83- min-height: 5px;
84- padding: 10px 25px 1px 50px;
85- overflow-x: hidden;
86- vertical-align: top;
87- text-align: left;
88- }
89- .docs pre {
90- margin: 15px 0 15px;
91- padding-left: 15px;
92- }
93- .docs p tt, .docs p code {
94- background: #f8f8ff;
95- border: 1px solid #dedede;
96- font-size: 12px;
97- padding: 0 0.2em;
98- }
99- .pilwrap {
100- position: relative;
101- }
102- .pilcrow {
103- font: 12px Arial;
104- text-decoration: none;
105- color: #454545;
106- position: absolute;
107- top: 3px; left: -20px;
108- padding: 1px 2px;
109- opacity: 0;
110- -webkit-transition: opacity 0.2s linear;
111- }
112- td.docs:hover .pilcrow {
113- opacity: 1;
114- }
115- td.code, th.code {
116- padding: 14px 15px 16px 25px;
117- width: 100%;
118- vertical-align: top;
119- background: #f5f5ff;
120- border-left: 1px solid #e5e5ee;
121- }
122- pre, tt, code {
123- font-size: 12px; line-height: 18px;
124- font-family: Menlo, Monaco, Consolas, "Lucida Console", monospace;
125- margin: 0; padding: 0;
126- }
127-
128-
129-/*---------------------- Syntax Highlighting -----------------------------*/
130-td.linenos { background-color: #000; padding-right: 10px; }
131-span.lineno { background-color: #000; padding: 0 5px 0 5px; }
132-body .hll { background-color: #ffffcc }
133-body .c { color: #408080; font-style: italic } /* Comment */
134-body .err { border: 1px solid #FF0000 } /* Error */
135-body .k { color: #954121 } /* Keyword */
136-body .o { color: #666666 } /* Operator */
137-body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
138-body .cp { color: #BC7A00 } /* Comment.Preproc */
139-body .c1 { color: #408080; font-style: italic } /* Comment.Single */
140-body .cs { color: #408080; font-style: italic } /* Comment.Special */
141-body .gd { color: #A00000 } /* Generic.Deleted */
142-body .ge { font-style: italic } /* Generic.Emph */
143-body .gr { color: #FF0000 } /* Generic.Error */
144-body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
145-body .gi { color: #00A000 } /* Generic.Inserted */
146-body .go { color: #808080 } /* Generic.Output */
147-body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
148-body .gs { font-weight: bold } /* Generic.Strong */
149-body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
150-body .gt { color: #0040D0 } /* Generic.Traceback */
151-body .kc { color: #954121 } /* Keyword.Constant */
152-body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
153-body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
154-body .kp { color: #954121 } /* Keyword.Pseudo */
155-body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
156-body .kt { color: #B00040 } /* Keyword.Type */
157-body .m { color: #666666 } /* Literal.Number */
158-body .s { color: #219161 } /* Literal.String */
159-body .na { color: #7D9029 } /* Name.Attribute */
160-body .nb { color: #954121 } /* Name.Builtin */
161-body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
162-body .no { color: #880000 } /* Name.Constant */
163-body .nd { color: #AA22FF } /* Name.Decorator */
164-body .ni { color: #999999; font-weight: bold } /* Name.Entity */
165-body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
166-body .nf { color: #0000FF } /* Name.Function */
167-body .nl { color: #A0A000 } /* Name.Label */
168-body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
169-body .nt { color: #954121; font-weight: bold } /* Name.Tag */
170-body .nv { color: #19469D } /* Name.Variable */
171-body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
172-body .w { color: #bbbbbb } /* Text.Whitespace */
173-body .mf { color: #666666 } /* Literal.Number.Float */
174-body .mh { color: #666666 } /* Literal.Number.Hex */
175-body .mi { color: #666666 } /* Literal.Number.Integer */
176-body .mo { color: #666666 } /* Literal.Number.Oct */
177-body .sb { color: #219161 } /* Literal.String.Backtick */
178-body .sc { color: #219161 } /* Literal.String.Char */
179-body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
180-body .s2 { color: #219161 } /* Literal.String.Double */
181-body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
182-body .sh { color: #219161 } /* Literal.String.Heredoc */
183-body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
184-body .sx { color: #954121 } /* Literal.String.Other */
185-body .sr { color: #BB6688 } /* Literal.String.Regex */
186-body .s1 { color: #219161 } /* Literal.String.Single */
187-body .ss { color: #19469D } /* Literal.String.Symbol */
188-body .bp { color: #954121 } /* Name.Builtin.Pseudo */
189-body .vc { color: #19469D } /* Name.Variable.Class */
190-body .vg { color: #19469D } /* Name.Variable.Global */
191-body .vi { color: #19469D } /* Name.Variable.Instance */
192-body .il { color: #666666 } /* Literal.Number.Integer.Long */
js/head.js+0−1
@@ -1 +0,0 @@
1-(function () {
js/tail.js+0−7
@@ -1,7 +0,0 @@
1- if ((module && module.exports) || exports) {
2- module = module || {};
3- exports = module.exports = algos;
4- } else {
5- window.algos = algos;
6- }
7-}());
lib/algos.js+135−194
@@ -1,6 +1,3 @@
1-/*jslint node:true browser:true */
2-'use strict';
3-var algos = {};
41 // Insertion sort, the stable sort.
52 // ================================
63 //
@@ -8,8 +5,6 @@ var algos = {};
85 // https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
96 // http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/
107
11-var insertionSort;
12-
138 // Ok, so what on earth is this? it's quite simple to explain visually. Think of bunch of cups,
149 // each one had a marble with a value written on it. To order the values on the balls we start
1510 // with the second cup from the left and take the ball out. Now compare the value of the loose
@@ -20,40 +15,28 @@ var insertionSort;
2015 // place the loose ball in the empty cup and move right to the next cup.
2116 // By the time you reach the last cup on the right, you'll have sorted the balls per their values
2217 // in ascending order.
18+export function insertionSort (list) {
19+ const len = list.length
20+ let val, i, hole
2321
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;
22+ for (i = 1; i < len; i += 1) {
23+ val = list[i]
24+ hole = i
4125
26+ while (hole > 0 && val < list[hole - 1]) {
27+ list[hole] = list[hole - 1]
28+ hole -= 1
4229 }
4330
44- return list;
45-
46-};
47-
31+ list[hole] = val
32+ }
4833
34+ return list
35+}
4936
5037 // Let's try merge sort, seems cool.
5138 // ================================
5239
53-var join,
54- mergeSort;
55-
56-
5740 // Join two sorted arrays together.
5841 //
5942 // Create two pointers, one for each list, point them both to the value
@@ -62,26 +45,23 @@ var join,
6245 // the indexes matches the length of it's list.
6346 // Simple pile up the remainder of the orther list at the end of the joined
6447 // 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- }
48+function join (a, b) {
49+ let joined = []
50+ let ai = 0
51+ let bi = 0
52+
53+ while (ai < a.length && bi < b.length) {
54+ if (a[ai] < b[bi]) {
55+ joined.push(a[ai])
56+ ai += 1
57+ } else {
58+ joined.push(b[bi])
59+ bi += 1
7960 }
61+ }
8062
81- return joined.concat(a.slice(ai).concat(b.slice(bi)));
82-
83-};
84-
63+ return joined.concat(a.slice(ai).concat(b.slice(bi)))
64+}
8565
8666 // Now for the cool part
8767 //
@@ -91,22 +71,17 @@ join = function (a, b) {
9171 // We return the recursive call, so that we keep on splitting smaller and
9272 // smaller lists until their are of a single item, and then we return them
9373 // as we pile them back up in order one by one.
74+export function mergeSort (list) {
75+ if (list.length <= 1) {
76+ return list
77+ }
9478
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-
79+ let m = Math.floor(list.length / 2)
80+ let a = list.slice(0, m)
81+ let b = list.slice(m)
10982
83+ return join(mergeSort(a), mergeSort(b))
84+}
11085
11186 // quick sort baby!
11287 // ================
@@ -114,13 +89,6 @@ mergeSort = algos.mergeSort = function (list) {
11489 // Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
11590 // much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
11691
117-var swap,
118- partition,
119- quickSort,
120- randomizedPartition,
121- randomizedQuickSort;
122-
123-
12492 // easy peasy swap function.
12593 //
12694 // think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
@@ -128,13 +96,11 @@ var swap,
12896 // pour the beer in the white one,
12997 // pour the wine in the red one,
13098 // pour the beer into the blue one.
131-
132-swap = function (list, first, second) {
133- var tmp = list[first];
134- list[first] = list[second];
135- list[second] = tmp;
136-};
137-
99+function swap (list, first, second) {
100+ const tmp = list[first]
101+ list[first] = list[second]
102+ list[second] = tmp
103+}
138104
139105 // creates a smaller partition of the array
140106 //
@@ -147,58 +113,53 @@ swap = function (list, first, second) {
147113 // move the pointers one more time in each direction so that we continue in the correct direction.
148114 // return the left pointer so it can be used as the starting point for the next partition.
149115
150-partition = function (list, left, right) {
151- var pivot = list[Math.floor( (left + right) / 2 )];
152-
153- while (left <= right) {
116+function partition (list, left, right) {
117+ const pivot = list[Math.floor((left + right) / 2)]
154118
155- while (list[left] < pivot) {
156- left += 1;
157- }
158-
159- while (list[right] > pivot) {
160- right -= 1;
161- }
162-
163- if (left <= right) {
164- swap(list, left, right);
165- left += 1;
166- right -= 1;
167- }
119+ while (left <= right) {
120+ while (list[left] < pivot) {
121+ left += 1
122+ }
168123
124+ while (list[right] > pivot) {
125+ right -= 1
169126 }
170127
171- return left;
172-};
128+ if (left <= right) {
129+ swap(list, left, right)
130+ left += 1
131+ right -= 1
132+ }
133+ }
173134
135+ return left
136+}
174137
175138 // Only operate on lists with 2 or more values.
176139 // use default values for left and right if they are undefined (first run).
177140 // partition the array.
178141 // if the left pointer can still be moved right towards the index, call quicksort again in that direction.
179142 // if the right pointer can be moved left, call quicksort from the current index.
143+export function quickSort (list, left, right) {
144+ let idx
Cut short: this commit's diff is over 1000 lines.
package.json+27−0
Cut short: this commit's diff is over 1000 lines.
test/levs.js+6−9
Cut short: this commit's diff is over 1000 lines.
test/sorts.js+24−26
Cut short: this commit's diff is over 1000 lines.