xonecas.io
2620
Ja

js-algos / commit 2bf7707

initial commit

xonecas2013-04-26 2bf7707d158910f9946e7b003da3f52573a35229

12 files changed

Makefile+25−0
@@ -0,0 +1,25 @@
1+all: lint docs module
2+
3+lint:
4+ @echo "----> Linting JS..."
5+ jslint --browser true --node true --white true {src/js,test}/*.js
6+ uglifyjs --lint {src/js,test}/*.js > /dev/null
7+
8+docs:
9+ @echo "----> Making docs..."
10+ docco -c css/docco.css src/js/*.js
11+
12+module:
13+ @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
16+
17+clean:
18+ rm -rf docs algos*
19+
20+test: lint module
21+ @echo "----> Testing..."
22+ node test/quicksort.js
23+ node test/mergesort.js
24+
25+.PHONY: docs module
README.md+32−0
@@ -0,0 +1,32 @@
1+Algorithms study.
2+=================
3+
4+I'm brushing up on these things.
5+
6+License
7+=======
8+
9+Everything needs a license right?
10+
11+The MIT License (MIT)
12+
13+Copyright (c) 2013 @xonecas
14+
15+Permission is hereby granted, free of charge, to any person obtaining a copy
16+of this software and associated documentation files (the "Software"), to deal
17+in the Software without restriction, including without limitation the rights
18+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19+copies of the Software, and to permit persons to whom the Software is
20+furnished to do so, subject to the following conditions:
21+
22+The above copyright notice and this permission notice shall be included in
23+all copies or substantial portions of the Software.
24+
25+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31+THE SOFTWARE.
32+
algos.js+72−0
@@ -0,0 +1,72 @@
1+(function(exports, global) {
2+ global["algos"] = exports;
3+ "use strict";
4+ var join, mergeSort;
5+ join = function(a, b) {
6+ var joined = [], ai = 0, bi = 0;
7+ while (ai < a.length && bi < b.length) {
8+ if (a[ai] < b[bi]) {
9+ joined.push(a[ai]);
10+ ai += 1;
11+ } else {
12+ joined.push(b[bi]);
13+ bi += 1;
14+ }
15+ }
16+ return joined.concat(a.slice(ai).concat(b.slice(bi)));
17+ };
18+ mergeSort = function(list) {
19+ if (list.length <= 1) {
20+ return list;
21+ }
22+ var m = Math.floor(list.length / 2), a = list.slice(0, m), b = list.slice(m);
23+ return join(mergeSort(a), mergeSort(b));
24+ };
25+ if (module) {
26+ module.exports.mergeSort = mergeSort;
27+ }
28+ "use strict";
29+ var swap, partition, quickSort;
30+ swap = function(list, first, second) {
31+ var tmp = list[first];
32+ list[first] = list[second];
33+ list[second] = tmp;
34+ };
35+ partition = function(list, left, right) {
36+ var pivot = list[Math.floor((left + right) / 2)];
37+ while (left <= right) {
38+ while (list[left] < pivot) {
39+ left += 1;
40+ }
41+ while (list[right] > pivot) {
42+ right -= 1;
43+ }
44+ if (left <= right) {
45+ swap(list, left, right);
46+ left += 1;
47+ right -= 1;
48+ }
49+ }
50+ return left;
51+ };
52+ quickSort = function(list, left, right) {
53+ var idx;
54+ if (list.length >= 2) {
55+ left = left === undefined ? 0 : left;
56+ right = right === undefined ? list.length - 1 : right;
57+ idx = partition(list, left, right);
58+ if (left < idx - 1) {
59+ quickSort(list, left, idx - 1);
60+ }
61+ if (idx < right) {
62+ quickSort(list, idx, right);
63+ }
64+ }
65+ return list;
66+ };
67+ if (module) {
68+ module.exports.quickSort = quickSort;
69+ }
70+})({}, function() {
71+ return this;
72+}());
algos.min.js+1−0
@@ -0,0 +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] …
css/docco.css+192−0
@@ -0,0 +1,192 @@
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/docco.css+192−0
@@ -0,0 +1,192 @@
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/mergesort.html+49−0
@@ -0,0 +1,49 @@
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>
5+
6+<p>Create two pointers, one for each list, point them both to the value
7+at index 0. Keep comparing the idexes values and keep the lowest in the
8+joined list, increment by 1 the respective index. Do this until one of
9+the indexes matches the length of it's list.
10+Simple pile up the remainder of the orther list at the end of the joined
11+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>
12+ <span class="kd">var</span> <span class="nx">joined</span> <span class="o">=</span> <span class="p">[],</span>
13+ <span class="nx">ai</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span>
14+ <span class="nx">bi</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
15+
16+ <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>
17+ <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>
18+ <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>
19+ <span class="nx">ai</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
20+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
21+ <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>
22+ <span class="nx">bi</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
23+ <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 …
25+
26+<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>
27+
28+<p>Don't you love recursion? Base case is a single item list, that needs no
29+sort and can be returned as is. The recursive step finds a middle to the
30+list and cuts it in half.
31+We return the recursive call, so that we keep on splitting smaller and
32+smaller lists until their are of a single item, and then we return them
33+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>
34+
35+ <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>
36+ <span class="k">return</span> <span class="nx">list</span><span class="p">;</span>
37+ <span class="p">}</span>
38+
39+ <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>
40+ <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>
41+ <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>
42+
43+ <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>
44+
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="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>
47+<span class="p">}</span>
48+
49+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
docs/quicksort.html+77−0
@@ -0,0 +1,77 @@
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 …
2+
3+<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>
7+ <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>
9+
10+<p>think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
11+we want the beer in the blue glass and the wine in the red one.
12+pour the beer in the white one,
13+pour the wine in the red one,
14+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>
15+ <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>
16+ <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>
17+ <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>
19+
20+<p>starting with a middle point because if the array is sorted you'll get
21+the worst possible performance if you choose the first or last elements.
22+while the left pointer is smaller or equal to the right pointer
23+move the left pointer to the right while it's value is less than the middle's value.
24+after, move the right pointer to the left while it's value is greater than the middle's value.
25+if the left pointer is smaller or equal to the right pointer swap the pointers values.
26+move the pointers one more time in each direction so that we continue in the correct direction.
27+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>
28+ <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>
29+
30+ <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>
31+
32+ <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>
33+ <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
34+ <span class="p">}</span>
35+
36+ <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>
37+ <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
38+ <span class="p">}</span>
39+
40+ <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>
41+ <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>
42+ <span class="nx">left</span> <span class="o">+=</span> <span class="mi">1</span><span class="p">;</span>
43+ <span class="nx">right</span> <span class="o">-=</span> <span class="mi">1</span><span class="p">;</span>
44+ <span class="p">}</span>
45+
46+ <span class="p">}</span>
47+
48+ <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.
50+use default values for left and right if they are undefined (first run).
51+partition the array.
52+if the left pointer can still be moved right towards the index, call quicksort again in that direction.
53+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>
54+ <span class="kd">var</span> <span class="nx">idx</span><span class="p">;</span>
55+
56+ <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>
57+
58+ <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>
59+ <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>
60+
61+ <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>
62+
63+ <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>
64+ <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>
65+ <span class="p">}</span>
66+
67+ <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>
68+ <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>
69+ <span class="p">}</span>
70+ <span class="p">}</span>
71+
72+ <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> …
74+ <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>
75+<span class="p">}</span>
76+
77+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
src/js/mergesort.js+63−0
@@ -0,0 +1,63 @@
1+// Let's try merge sort, seems cool.
2+'use strict';
3+
4+var join,
5+ mergeSort;
6+
7+
8+// Join two sorted arrays together.
9+//
10+// Create two pointers, one for each list, point them both to the value
11+// at index 0. Keep comparing the idexes values and keep the lowest in the
12+// joined list, increment by 1 the respective index. Do this until one of
13+// the indexes matches the length of it's list.
14+// Simple pile up the remainder of the orther list at the end of the joined
15+// result. Ta-Da!
16+
17+join = function (a, b) {
18+ var joined = [],
19+ ai = 0,
20+ bi = 0;
21+
22+ while (ai < a.length && bi < b.length) {
23+ if (a[ai] < b[bi]) {
24+ joined.push(a[ai]);
25+ ai += 1;
26+ } else {
27+ joined.push(b[bi]);
28+ bi += 1;
29+ }
30+ }
31+
32+ return joined.concat(a.slice(ai).concat(b.slice(bi)));
33+
34+};
35+
36+
37+// Now for the cool part
38+//
39+// Don't you love recursion? Base case is a single item list, that needs no
40+// sort and can be returned as is. The recursive step finds a middle to the
41+// list and cuts it in half.
42+// We return the recursive call, so that we keep on splitting smaller and
43+// smaller lists until their are of a single item, and then we return them
44+// as we pile them back up in order one by one.
45+
46+mergeSort = function (list) {
47+
48+ if (list.length <= 1) {
49+ return list;
50+ }
51+
52+ var m = Math.floor(list.length / 2),
53+ a = list.slice(0, m),
54+ b = list.slice(m);
55+
56+ return join(mergeSort(a), mergeSort(b));
57+
58+};
59+
60+// expose as the global/module (part of the build process with uglifyjs
61+if (module) {
62+ module.exports.mergeSort = mergeSort;
63+}
src/js/quicksort.js+97−0
@@ -0,0 +1,97 @@
1+// quick sort baby!
2+//
3+// 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/
5+'use strict';
6+
7+var swap,
8+ partition,
9+ quickSort;
10+
11+
12+// easy peasy swap function.
13+//
14+// think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
15+// we want the beer in the blue glass and the wine in the red one.
16+// pour the beer in the white one,
17+// pour the wine in the red one,
18+// pour the beer into the blue one.
19+
20+swap = function (list, first, second) {
21+ var tmp = list[first];
22+ list[first] = list[second];
23+ list[second] = tmp;
24+};
25+
26+
27+// creates a smaller partition of the array
28+//
29+// starting with a middle point because if the array is sorted you'll get
30+// the worst possible performance if you choose the first or last elements.
31+// while the left pointer is smaller or equal to the right pointer
32+// move the left pointer to the right while it's value is less than the middle's value.
33+// after, move the right pointer to the left while it's value is greater than the middle's value.
34+// if the left pointer is smaller or equal to the right pointer swap the pointers values.
35+// move the pointers one more time in each direction so that we continue in the correct direction.
36+// return the left pointer so it can be used as the starting point for the next partition.
37+
38+partition = function (list, left, right) {
39+ var pivot = list[Math.floor( (left + right) / 2 )];
40+
41+ while (left <= right) {
42+
43+ while (list[left] < pivot) {
44+ left += 1;
45+ }
46+
47+ while (list[right] > pivot) {
48+ right -= 1;
49+ }
50+
51+ if (left <= right) {
52+ swap(list, left, right);
53+ left += 1;
54+ right -= 1;
55+ }
56+
57+ }
58+
59+ return left;
60+};
61+
62+
63+// Only operate on lists with 2 or more values.
64+// use default values for left and right if they are undefined (first run).
65+// partition the array.
66+// if the left pointer can still be moved right towards the index, call quicksort again in that direction.
67+// if the right pointer can be moved left, call quicksort from the current index.
68+
69+quickSort = function (list, left, right) {
70+ var idx;
71+
72+ if (list.length >= 2) {
73+
74+ left = left === undefined ? 0 : left;
75+ right = right === undefined ? list.length - 1 : right;
76+
77+ idx = partition(list, left, right);
78+
79+ if (left < idx - 1) {
80+ quickSort(list, left, idx - 1);
81+ }
82+
83+ if (idx < right) {
84+ quickSort(list, idx, right);
85+ }
86+ }
87+
88+ return list;
89+};
90+
91+
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) {
96+ module.exports.quickSort = quickSort;
97+}
test/mergesort.js+7−0
@@ -0,0 +1,7 @@
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+7−0
@@ -0,0 +1,7 @@
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));