Skip to content

Commit 297875e

Browse files
authoredMay 24, 2018
Merge branch 'master' into master
2 parents f2aebe7 + d596e1d commit 297875e

File tree

13 files changed

+254
-40
lines changed

13 files changed

+254
-40
lines changed
 

‎README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Each algorithm and data structure have its own separate README
1010
with related explanations and links for further reading and YouTube
1111
videos.
1212

13+
Read this in other languages: [Chinese](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md)
14+
1315
## Data Structures
1416

1517
Data structure is a particular way of organizing and storing data in a computer so that it can
@@ -190,7 +192,7 @@ Below is the list of some of the most used Big O notations and their performance
190192
| **O(1)** | 1 | 1 | 1 |
191193
| **O(log N)** | 3 | 6 | 9 |
192194
| **O(N)** | 10 | 100 | 1000 |
193-
| **O(N log N)** | 30 | 60 | 9000 |
195+
| **O(N log N)** | 30 | 600 | 9000 |
194196
| **O(N^2)** | 100 | 10000 | 1000000 |
195197
| **O(2^N)** | 1024 | 1.26e+29 | 1.07e+301 |
196198
| **O(N!)** | 3628800 | 9.3e+157 | 4.02e+2567 |

‎README.zh-CN.md

+212
Large diffs are not rendered by default.

‎src/algorithms/search/binary-search/binarySearch.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function binarySearch(sortedArray, seekElement, comparatorCallbac
2222
}
2323

2424
// Decide which half to choose for seeking next: left or right one.
25-
if (comparator.lessThen(sortedArray[middleIndex], seekElement)) {
25+
if (comparator.lessThan(sortedArray[middleIndex], seekElement)) {
2626
// Go to the right half of the array.
2727
startIndex = middleIndex + 1;
2828
} else {

‎src/algorithms/sorting/bubble-sort/BubbleSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default class BubbleSort extends Sort {
1818
this.callbacks.visitingCallback(array[j]);
1919

2020
// Swap elements if they are in wrong order.
21-
if (this.comparator.lessThen(array[j + 1], array[j])) {
21+
if (this.comparator.lessThan(array[j + 1], array[j])) {
2222
const tmp = array[j + 1];
2323
array[j + 1] = array[j];
2424
array[j] = tmp;

‎src/algorithms/sorting/insertion-sort/InsertionSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class InsertionSort extends Sort {
1515
// If this is the case then swap that elements.
1616
while (
1717
array[currentIndex - 1] &&
18-
this.comparator.lessThen(array[currentIndex], array[currentIndex - 1])
18+
this.comparator.lessThan(array[currentIndex], array[currentIndex - 1])
1919
) {
2020
// Call visiting callback.
2121
this.callbacks.visitingCallback(array[currentIndex - 1]);

‎src/algorithms/sorting/merge-sort/MergeSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default class MergeSort extends Sort {
3131
let minimumElement = null;
3232

3333
// Find minimum element of two arrays.
34-
if (this.comparator.lessThenOrEqual(leftArray[0], rightArray[0])) {
34+
if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) {
3535
minimumElement = leftArray.shift();
3636
} else {
3737
minimumElement = rightArray.shift();

‎src/algorithms/sorting/quick-sort/QuickSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class QuickSort extends Sort {
2727

2828
if (this.comparator.equal(currentElement, pivotElement)) {
2929
centerArray.push(currentElement);
30-
} else if (this.comparator.lessThen(currentElement, pivotElement)) {
30+
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
3131
leftArray.push(currentElement);
3232
} else {
3333
rightArray.push(currentElement);

‎src/algorithms/sorting/selection-sort/SelectionSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default class SelectionSort extends Sort {
1616
// Call visiting callback.
1717
this.callbacks.visitingCallback(array[j]);
1818

19-
if (this.comparator.lessThen(array[j], array[minIndex])) {
19+
if (this.comparator.lessThan(array[j], array[minIndex])) {
2020
minIndex = j;
2121
}
2222
}

‎src/algorithms/sorting/shell-sort/ShellSort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default class ShellSort extends Sort {
2020
this.callbacks.visitingCallback(array[currentIndex]);
2121

2222
// Compare and swap array elements if needed.
23-
if (this.comparator.lessThen(array[gapShiftedIndex], array[currentIndex])) {
23+
if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) {
2424
const tmp = array[currentIndex];
2525
array[currentIndex] = array[gapShiftedIndex];
2626
array[gapShiftedIndex] = tmp;

‎src/algorithms/uncategorized/knight-tour/knightTour.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @return {number[][]}
55
*/
66
function getPossibleMoves(chessboard, position) {
7-
// Generate all knight moves (event those that goes beyond the board).
7+
// Generate all knight moves (even those that go beyond the board).
88
const possibleMoves = [
99
[position[0] - 1, position[1] - 2],
1010
[position[0] - 2, position[1] - 1],

‎src/data-structures/heap/MinHeap.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export default class MinHeap {
167167
leftChild !== null &&
168168
(
169169
parentItem === null ||
170-
this.compare.lessThen(parentItem, this.heapContainer[indexToRemove])
170+
this.compare.lessThan(parentItem, this.heapContainer[indexToRemove])
171171
)
172172
) {
173173
this.heapifyDown(indexToRemove);
@@ -209,7 +209,7 @@ export default class MinHeap {
209209

210210
while (
211211
this.hasParent(currentIndex) &&
212-
this.compare.lessThen(this.heapContainer[currentIndex], this.parent(currentIndex))
212+
this.compare.lessThan(this.heapContainer[currentIndex], this.parent(currentIndex))
213213
) {
214214
this.swap(currentIndex, this.getParentIndex(currentIndex));
215215
currentIndex = this.getParentIndex(currentIndex);
@@ -228,14 +228,14 @@ export default class MinHeap {
228228
while (this.hasLeftChild(currentIndex)) {
229229
if (
230230
this.hasRightChild(currentIndex) &&
231-
this.compare.lessThen(this.rightChild(currentIndex), this.leftChild(currentIndex))
231+
this.compare.lessThan(this.rightChild(currentIndex), this.leftChild(currentIndex))
232232
) {
233233
nextIndex = this.getRightChildIndex(currentIndex);
234234
} else {
235235
nextIndex = this.getLeftChildIndex(currentIndex);
236236
}
237237

238-
if (this.compare.lessThen(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
238+
if (this.compare.lessThan(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
239239
break;
240240
}
241241

‎src/utils/comparator/Comparator.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ export default class Comparator {
2323
return this.compare(a, b) === 0;
2424
}
2525

26-
lessThen(a, b) {
26+
lessThan(a, b) {
2727
return this.compare(a, b) < 0;
2828
}
2929

30-
greaterThen(a, b) {
30+
greaterThan(a, b) {
3131
return this.compare(a, b) > 0;
3232
}
3333

34-
lessThenOrEqual(a, b) {
35-
return this.lessThen(a, b) || this.equal(a, b);
34+
lessThanOrEqual(a, b) {
35+
return this.lessThan(a, b) || this.equal(a, b);
3636
}
3737

38-
greaterThenOrEqual(a, b) {
39-
return this.greaterThen(a, b) || this.equal(a, b);
38+
greaterThanOrEqual(a, b) {
39+
return this.greaterThan(a, b) || this.equal(a, b);
4040
}
4141

4242
reverse() {

‎src/utils/comparator/__test__/Comparator.test.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ describe('Comparator', () => {
77
expect(comparator.equal(0, 0)).toBeTruthy();
88
expect(comparator.equal(0, 1)).toBeFalsy();
99
expect(comparator.equal('a', 'a')).toBeTruthy();
10-
expect(comparator.lessThen(1, 2)).toBeTruthy();
11-
expect(comparator.lessThen(-1, 2)).toBeTruthy();
12-
expect(comparator.lessThen('a', 'b')).toBeTruthy();
13-
expect(comparator.lessThen('a', 'ab')).toBeTruthy();
14-
expect(comparator.lessThen(10, 2)).toBeFalsy();
15-
expect(comparator.lessThenOrEqual(10, 2)).toBeFalsy();
16-
expect(comparator.lessThenOrEqual(1, 1)).toBeTruthy();
17-
expect(comparator.lessThenOrEqual(0, 0)).toBeTruthy();
18-
expect(comparator.greaterThen(0, 0)).toBeFalsy();
19-
expect(comparator.greaterThen(10, 0)).toBeTruthy();
20-
expect(comparator.greaterThenOrEqual(10, 0)).toBeTruthy();
21-
expect(comparator.greaterThenOrEqual(10, 10)).toBeTruthy();
22-
expect(comparator.greaterThenOrEqual(0, 10)).toBeFalsy();
10+
expect(comparator.lessThan(1, 2)).toBeTruthy();
11+
expect(comparator.lessThan(-1, 2)).toBeTruthy();
12+
expect(comparator.lessThan('a', 'b')).toBeTruthy();
13+
expect(comparator.lessThan('a', 'ab')).toBeTruthy();
14+
expect(comparator.lessThan(10, 2)).toBeFalsy();
15+
expect(comparator.lessThanOrEqual(10, 2)).toBeFalsy();
16+
expect(comparator.lessThanOrEqual(1, 1)).toBeTruthy();
17+
expect(comparator.lessThanOrEqual(0, 0)).toBeTruthy();
18+
expect(comparator.greaterThan(0, 0)).toBeFalsy();
19+
expect(comparator.greaterThan(10, 0)).toBeTruthy();
20+
expect(comparator.greaterThanOrEqual(10, 0)).toBeTruthy();
21+
expect(comparator.greaterThanOrEqual(10, 10)).toBeTruthy();
22+
expect(comparator.greaterThanOrEqual(0, 10)).toBeFalsy();
2323
});
2424

2525
it('should compare with custom comparator function', () => {
@@ -33,18 +33,18 @@ describe('Comparator', () => {
3333

3434
expect(comparator.equal('a', 'b')).toBeTruthy();
3535
expect(comparator.equal('a', '')).toBeFalsy();
36-
expect(comparator.lessThen('b', 'aa')).toBeTruthy();
37-
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeFalsy();
38-
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeTruthy();
39-
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
36+
expect(comparator.lessThan('b', 'aa')).toBeTruthy();
37+
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeFalsy();
38+
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeTruthy();
39+
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
4040

4141
comparator.reverse();
4242

4343
expect(comparator.equal('a', 'b')).toBeTruthy();
4444
expect(comparator.equal('a', '')).toBeFalsy();
45-
expect(comparator.lessThen('b', 'aa')).toBeFalsy();
46-
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeTruthy();
47-
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeFalsy();
48-
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
45+
expect(comparator.lessThan('b', 'aa')).toBeFalsy();
46+
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeTruthy();
47+
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeFalsy();
48+
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
4949
});
5050
});

0 commit comments

Comments
 (0)
Please sign in to comment.