@@ -1,4 +1,3 @@
1-var algos = {};
21 // Insertion sort, the stable sort.
32 // ================================
43 //
@@ -6,8 +5,6 @@ var algos = {};
65 // https://www.khanacademy.org/science/computer-science/v/insertion-sort-algorithm
76 // http://www.nczonline.net/blog/2012/09/17/computer-science-in-javascript-insertion-sort/
87
9-var insertionSort;
10-
118 // Ok, so what on earth is this? it's quite simple to explain visually. Think of bunch of cups,
129 // each one had a marble with a value written on it. To order the values on the balls we start
1310 // with the second cup from the left and take the ball out. Now compare the value of the loose
@@ -18,40 +15,28 @@ var insertionSort;
1815 // place the loose ball in the empty cup and move right to the next cup.
1916 // By the time you reach the last cup on the right, you'll have sorted the balls per their values
2017 // in ascending order.
18+export function insertionSort (list) {
19+ const len = list.length
20+ let val, i, hole
2121
22-insertionSort = algos.insertionSort = function (list) {
23- var len = list.length,
24- val,
25- i,
26- hole;
27-
28- for (i = 1; i < len; i += 1) {
29-
30- val = list[i];
31- hole = i;
32-
33- while (hole > 0 && val < list[hole - 1]) {
34- list[hole] = list[hole - 1];
35- hole -= 1;
36- }
37-
38- list[hole] = val;
22+ for (i = 1; i < len; i += 1) {
23+ val = list[i]
24+ hole = i
3925
26+ while (hole > 0 && val < list[hole - 1]) {
27+ list[hole] = list[hole - 1]
28+ hole -= 1
4029 }
4130
42- return list;
43-
44-};
45-
31+ list[hole] = val
32+ }
4633
34+ return list
35+}
4736
4837 // Let's try merge sort, seems cool.
4938 // ================================
5039
51-var join,
52- mergeSort;
53-
54-
5540 // Join two sorted arrays together.
5641 //
5742 // Create two pointers, one for each list, point them both to the value
@@ -60,26 +45,23 @@ var join,
6045 // the indexes matches the length of it's list.
6146 // Simple pile up the remainder of the orther list at the end of the joined
6247 // result. Ta-Da!
63-
64-join = function (a, b) {
65- var joined = [],
66- ai = 0,
67- bi = 0;
68-
69- while (ai < a.length && bi < b.length) {
70- if (a[ai] < b[bi]) {
71- joined.push(a[ai]);
72- ai += 1;
73- } else {
74- joined.push(b[bi]);
75- bi += 1;
76- }
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
7760 }
61+ }
7862
79- return joined.concat(a.slice(ai).concat(b.slice(bi)));
80-
81-};
82-
63+ return joined.concat(a.slice(ai).concat(b.slice(bi)))
64+}
8365
8466 // Now for the cool part
8567 //
@@ -89,22 +71,17 @@ join = function (a, b) {
8971 // We return the recursive call, so that we keep on splitting smaller and
9072 // smaller lists until their are of a single item, and then we return them
9173 // 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+ }
9278
93-mergeSort = algos.mergeSort = function (list) {
94-
95- if (list.length <= 1) {
96- return list;
97- }
98-
99- var m = Math.floor(list.length / 2),
100- a = list.slice(0, m),
101- b = list.slice(m);
102-
103- return join(mergeSort(a), mergeSort(b));
104-
105-};
106-
79+ let m = Math.floor(list.length / 2)
80+ let a = list.slice(0, m)
81+ let b = list.slice(m)
10782
83+ return join(mergeSort(a), mergeSort(b))
84+}
10885
10986 // quick sort baby!
11087 // ================
@@ -112,13 +89,6 @@ mergeSort = algos.mergeSort = function (list) {
11289 // Chome's V8 uses this for Array.sort() for larger arrays (length 20ish or more)
11390 // much more to be found about quicksort here http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/
11491
115-var swap,
116- partition,
117- quickSort,
118- randomizedPartition,
119- randomizedQuickSort;
120-
121-
12292 // easy peasy swap function.
12393 //
12494 // think 3 glasses, a red one with beer, a blue one with wine, and a white empty one.
@@ -126,13 +96,11 @@ var swap,
12696 // pour the beer in the white one,
12797 // pour the wine in the red one,
12898 // pour the beer into the blue one.
129-
130-swap = function (list, first, second) {
131- var tmp = list[first];
132- list[first] = list[second];
133- list[second] = tmp;
134-};
135-
99+function swap (list, first, second) {
100+ const tmp = list[first]
101+ list[first] = list[second]
102+ list[second] = tmp
103+}
136104
137105 // creates a smaller partition of the array
138106 //
@@ -145,58 +113,53 @@ swap = function (list, first, second) {
145113 // move the pointers one more time in each direction so that we continue in the correct direction.
146114 // return the left pointer so it can be used as the starting point for the next partition.
147115
148-partition = function (list, left, right) {
149- var pivot = list[Math.floor( (left + right) / 2 )];
150-
151- while (left <= right) {
116+function partition (list, left, right) {
117+ const pivot = list[Math.floor((left + right) / 2)]
152118
153- while (list[left] < pivot) {
154- left += 1;
155- }
156-
157- while (list[right] > pivot) {
158- right -= 1;
159- }
160-
161- if (left <= right) {
162- swap(list, left, right);
163- left += 1;
164- right -= 1;
165- }
119+ while (left <= right) {
120+ while (list[left] < pivot) {
121+ left += 1
122+ }
166123
124+ while (list[right] > pivot) {
125+ right -= 1
167126 }
168127
169- return left;
170-};
128+ if (left <= right) {
129+ swap(list, left, right)
130+ left += 1
131+ right -= 1
132+ }
133+ }
171134
135+ return left
136+}
172137
173138 // Only operate on lists with 2 or more values.
174139 // use default values for left and right if they are undefined (first run).
175140 // partition the array.
176141 // if the left pointer can still be moved right towards the index, call quicksort again in that direction.
177142 // if the right pointer can be moved left, call quicksort from the current index.
143+export function quickSort (list, left, right) {
144+ let idx
178145
179-quickSort = algos.quickSort = function (list, left, right) {
180- var idx;
181-
182- if (list.length >= 2) {
146+ if (list.length >= 2) {
147+ left = left === undefined ? 0 : left
148+ right = right === undefined ? list.length - 1 : right
183149
184- left = left === undefined ? 0 : left;
185- right = right === undefined ? list.length - 1 : right;
150+ idx = partition(list, left, right)
186151
187- idx = partition(list, left, right);
188-
189- if (left < idx - 1) {
190- quickSort(list, left, idx - 1);
191- }
152+ if (left < idx - 1) {
153+ quickSort(list, left, idx - 1)
154+ }
192155
193- if (idx < right) {
194- quickSort(list, idx, right);
195- }
156+ if (idx < right) {
157+ quickSort(list, idx, right)
196158 }
159+ }
197160
198- return list;
199-};
161+ return list
162+}
200163
201164 // This version of partition and quick sort calculates the pivot at random,
202165 // since the probability of the randomizer finding an item in the list that divides
@@ -204,126 +167,106 @@ quickSort = algos.quickSort = function (list, left, right) {
204167 // The expected time requirement becomes O(n log n), and the worst-case O(n^2) is very
205168 // unlikely to happen
206169
207-// var randomizedPartition,
208- // randomizedQuickSort;
209-
210-randomizedPartition = function (list, left, right) {
211- var pivot = list[Math.floor(Math.random() * (right - left) + left)];
212-
213- while (left <= right) {
214-
215- while (list[left] < pivot) {
216- left += 1;
217- }
218-
219- while (list[right] > pivot) {
220- right -= 1;
221- }
222-
223- if (left <= right) {
224- swap(list, left, right);
225- left += 1;
226- right -= 1;
227- }
170+// First the function to randomly partition the list
171+function randomizedPartition (list, left, right) {
172+ const pivot = list[Math.floor(Math.random() * (right - left) + left)]
228173
174+ while (left <= right) {
175+ while (list[left] < pivot) {
176+ left += 1
229177 }
230178
231- return left;
232-};
179+ while (list[right] > pivot) {
180+ right -= 1
181+ }
233182
234-randomizedQuickSort = algos.randomizedQuickSort = function (list, left, right) {
235- var idx;
183+ if (left <= right) {
184+ swap(list, left, right)
185+ left += 1
186+ right -= 1
187+ }
188+ }
236189
237- if (list.length >= 2) {
190+ return left
191+}
238192
239- left = left === undefined ? 0 : left;
240- right = right === undefined ? list.length - 1 : right;
193+export function randomizedQuickSort (list, left, right) {
194+ let idx
241195
242- idx = randomizedPartition(list, left, right);
196+ if (list.length >= 2) {
197+ left = left === undefined ? 0 : left
198+ right = right === undefined ? list.length - 1 : right
243199
244- if (left < idx - 1) {
245- randomizedQuickSort(list, left, idx - 1);
246- }
200+ idx = randomizedPartition(list, left, right)
247201
248- if (idx < right) {
249- randomizedQuickSort(list, idx, right);
250- }
202+ if (left < idx - 1) {
203+ randomizedQuickSort(list, left, idx - 1)
251204 }
252205
253- return list;
254-};
206+ if (idx < right) {
207+ randomizedQuickSort(list, idx, right)
208+ }
209+ }
255210
211+ return list
212+}
256213
257214 // Bubble sort, aka blubbly
258215 // ========================
259216
260-var bubbleSort;
261-
262-
263217 // As the name says, bubbly over here compares all the values in sequence, "bubbling" the
264218 // higher values to the top. VERY SLOW! n^2 slow...
265219
266-bubbleSort = algos.bubbleSort = function (list) {
267- var len = list.length,
268- i,
269- j;
270-
271- for (i = 0; i < len; i += 1) {
272- for (j = 0; j < (len - i); j += 1) {
273- if (list[j] > list[j + 1]) {
274- swap(list, j, j + 1);
275- }
276- }
277- }
220+export function bubbleSort (list) {
221+ const len = list.length
222+ let i, j
278223
279- return list;
280-};
224+ for (i = 0; i < len; i += 1) {
225+ for (j = 0; j < (len - i); j += 1) {
226+ if (list[j] > list[j + 1]) {
227+ swap(list, j, j + 1)
228+ }
229+ }
230+ }
281231
232+ return list
233+}
282234
283235 // shuffle, make a mess of things...
284236 // =================================
285237
286-var shuffle;
287-
288-
289238 // slow and compreensive? fast and simple?
290239 // for each item in the array, create a random number within the length and swap.
291240
292-shuffle = algos.shuffle = function (list) {
293- var len = list.length,
294- max = len - 1;
295-
296- while (len) {
297- len -= 1;
298- swap(list, len, Math.floor( Math.random() * max ));
299- }
241+export function shuffle (list) {
242+ let len = list.length
243+ const max = len - 1
300244
301- return list;
302-};
245+ while (len) {
246+ len -= 1
247+ swap(list, len, Math.floor(Math.random() * max))
248+ }
303249
250+ return list
251+}
304252
305253 // Levenshtein distance (fuzzy text matching)
306254 // ==========================================
307255
308-var fuzzy;
309-
310-
311256 // Levenshtein distance is the number of edits to change one word into another. For example
312257 // `git` and `got` have a Levenshtein distance of 1, since only one edit needs to be made
313258 // to tranform them into equal strings. Levenshtein is not very efficient and for performance
314259 // concerns it should only be applied to small strings at a time, since checking a massive text
315260 // would take forever(ish).
316261
317-fuzzy = algos.fuzzy = function (a, b) {
318-
319- // base cases
320- if (a === b) { return 0; }
321- if (!a.length || !b.length) { return 0; }
322-
323- return Math.min(
324- fuzzy(a.substring(1), b) + 1,
325- fuzzy(b.substring(1), a) + 1,
326- fuzzy(a.substring(1), b.substring(1)) + (a[0] !== b[0] ? 1 : 0)
327- );
328-
329-};
262+export function fuzzy (a, b) {
263+ // base cases
264+ if (a === b) { return 0 }
265+ if (!a.length || !b.length) { return 0 }
266+
267+ return Math.min(
268+ fuzzy(a.substring(1), b) + 1,
269+ fuzzy(b.substring(1), a) + 1,
270+ fuzzy(a.substring(1), b.substring(1)) + (a[0] !== b[0] ? 1 : 0)
271+ )
272+}