Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
trekhleb authored May 24, 2018
2 parents f2aebe7 + d596e1d commit 297875e
Show file tree
Hide file tree
Showing 13 changed files with 254 additions and 40 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Each algorithm and data structure have its own separate README
with related explanations and links for further reading and YouTube
videos.

Read this in other languages: [Chinese](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md)

## Data Structures

Data structure is a particular way of organizing and storing data in a computer so that it can
Expand Down Expand Up @@ -190,7 +192,7 @@ Below is the list of some of the most used Big O notations and their performance
| **O(1)** | 1 | 1 | 1 |
| **O(log N)** | 3 | 6 | 9 |
| **O(N)** | 10 | 100 | 1000 |
| **O(N log N)** | 30 | 60 | 9000 |
| **O(N log N)** | 30 | 600 | 9000 |
| **O(N^2)** | 100 | 10000 | 1000000 |
| **O(2^N)** | 1024 | 1.26e+29 | 1.07e+301 |
| **O(N!)** | 3628800 | 9.3e+157 | 4.02e+2567 |
Expand Down
212 changes: 212 additions & 0 deletions README.zh-CN.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/algorithms/search/binary-search/binarySearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function binarySearch(sortedArray, seekElement, comparatorCallbac
}

// Decide which half to choose for seeking next: left or right one.
if (comparator.lessThen(sortedArray[middleIndex], seekElement)) {
if (comparator.lessThan(sortedArray[middleIndex], seekElement)) {
// Go to the right half of the array.
startIndex = middleIndex + 1;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/bubble-sort/BubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class BubbleSort extends Sort {
this.callbacks.visitingCallback(array[j]);

// Swap elements if they are in wrong order.
if (this.comparator.lessThen(array[j + 1], array[j])) {
if (this.comparator.lessThan(array[j + 1], array[j])) {
const tmp = array[j + 1];
array[j + 1] = array[j];
array[j] = tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/insertion-sort/InsertionSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class InsertionSort extends Sort {
// If this is the case then swap that elements.
while (
array[currentIndex - 1] &&
this.comparator.lessThen(array[currentIndex], array[currentIndex - 1])
this.comparator.lessThan(array[currentIndex], array[currentIndex - 1])
) {
// Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex - 1]);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/merge-sort/MergeSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class MergeSort extends Sort {
let minimumElement = null;

// Find minimum element of two arrays.
if (this.comparator.lessThenOrEqual(leftArray[0], rightArray[0])) {
if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) {
minimumElement = leftArray.shift();
} else {
minimumElement = rightArray.shift();
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/quick-sort/QuickSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default class QuickSort extends Sort {

if (this.comparator.equal(currentElement, pivotElement)) {
centerArray.push(currentElement);
} else if (this.comparator.lessThen(currentElement, pivotElement)) {
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
leftArray.push(currentElement);
} else {
rightArray.push(currentElement);
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/selection-sort/SelectionSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class SelectionSort extends Sort {
// Call visiting callback.
this.callbacks.visitingCallback(array[j]);

if (this.comparator.lessThen(array[j], array[minIndex])) {
if (this.comparator.lessThan(array[j], array[minIndex])) {
minIndex = j;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/sorting/shell-sort/ShellSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class ShellSort extends Sort {
this.callbacks.visitingCallback(array[currentIndex]);

// Compare and swap array elements if needed.
if (this.comparator.lessThen(array[gapShiftedIndex], array[currentIndex])) {
if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) {
const tmp = array[currentIndex];
array[currentIndex] = array[gapShiftedIndex];
array[gapShiftedIndex] = tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/uncategorized/knight-tour/knightTour.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @return {number[][]}
*/
function getPossibleMoves(chessboard, position) {
// Generate all knight moves (event those that goes beyond the board).
// Generate all knight moves (even those that go beyond the board).
const possibleMoves = [
[position[0] - 1, position[1] - 2],
[position[0] - 2, position[1] - 1],
Expand Down
8 changes: 4 additions & 4 deletions src/data-structures/heap/MinHeap.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default class MinHeap {
leftChild !== null &&
(
parentItem === null ||
this.compare.lessThen(parentItem, this.heapContainer[indexToRemove])
this.compare.lessThan(parentItem, this.heapContainer[indexToRemove])
)
) {
this.heapifyDown(indexToRemove);
Expand Down Expand Up @@ -209,7 +209,7 @@ export default class MinHeap {

while (
this.hasParent(currentIndex) &&
this.compare.lessThen(this.heapContainer[currentIndex], this.parent(currentIndex))
this.compare.lessThan(this.heapContainer[currentIndex], this.parent(currentIndex))
) {
this.swap(currentIndex, this.getParentIndex(currentIndex));
currentIndex = this.getParentIndex(currentIndex);
Expand All @@ -228,14 +228,14 @@ export default class MinHeap {
while (this.hasLeftChild(currentIndex)) {
if (
this.hasRightChild(currentIndex) &&
this.compare.lessThen(this.rightChild(currentIndex), this.leftChild(currentIndex))
this.compare.lessThan(this.rightChild(currentIndex), this.leftChild(currentIndex))
) {
nextIndex = this.getRightChildIndex(currentIndex);
} else {
nextIndex = this.getLeftChildIndex(currentIndex);
}

if (this.compare.lessThen(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
if (this.compare.lessThan(this.heapContainer[currentIndex], this.heapContainer[nextIndex])) {
break;
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils/comparator/Comparator.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ export default class Comparator {
return this.compare(a, b) === 0;
}

lessThen(a, b) {
lessThan(a, b) {
return this.compare(a, b) < 0;
}

greaterThen(a, b) {
greaterThan(a, b) {
return this.compare(a, b) > 0;
}

lessThenOrEqual(a, b) {
return this.lessThen(a, b) || this.equal(a, b);
lessThanOrEqual(a, b) {
return this.lessThan(a, b) || this.equal(a, b);
}

greaterThenOrEqual(a, b) {
return this.greaterThen(a, b) || this.equal(a, b);
greaterThanOrEqual(a, b) {
return this.greaterThan(a, b) || this.equal(a, b);
}

reverse() {
Expand Down
42 changes: 21 additions & 21 deletions src/utils/comparator/__test__/Comparator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ describe('Comparator', () => {
expect(comparator.equal(0, 0)).toBeTruthy();
expect(comparator.equal(0, 1)).toBeFalsy();
expect(comparator.equal('a', 'a')).toBeTruthy();
expect(comparator.lessThen(1, 2)).toBeTruthy();
expect(comparator.lessThen(-1, 2)).toBeTruthy();
expect(comparator.lessThen('a', 'b')).toBeTruthy();
expect(comparator.lessThen('a', 'ab')).toBeTruthy();
expect(comparator.lessThen(10, 2)).toBeFalsy();
expect(comparator.lessThenOrEqual(10, 2)).toBeFalsy();
expect(comparator.lessThenOrEqual(1, 1)).toBeTruthy();
expect(comparator.lessThenOrEqual(0, 0)).toBeTruthy();
expect(comparator.greaterThen(0, 0)).toBeFalsy();
expect(comparator.greaterThen(10, 0)).toBeTruthy();
expect(comparator.greaterThenOrEqual(10, 0)).toBeTruthy();
expect(comparator.greaterThenOrEqual(10, 10)).toBeTruthy();
expect(comparator.greaterThenOrEqual(0, 10)).toBeFalsy();
expect(comparator.lessThan(1, 2)).toBeTruthy();
expect(comparator.lessThan(-1, 2)).toBeTruthy();
expect(comparator.lessThan('a', 'b')).toBeTruthy();
expect(comparator.lessThan('a', 'ab')).toBeTruthy();
expect(comparator.lessThan(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(10, 2)).toBeFalsy();
expect(comparator.lessThanOrEqual(1, 1)).toBeTruthy();
expect(comparator.lessThanOrEqual(0, 0)).toBeTruthy();
expect(comparator.greaterThan(0, 0)).toBeFalsy();
expect(comparator.greaterThan(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 0)).toBeTruthy();
expect(comparator.greaterThanOrEqual(10, 10)).toBeTruthy();
expect(comparator.greaterThanOrEqual(0, 10)).toBeFalsy();
});

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

expect(comparator.equal('a', 'b')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThen('b', 'aa')).toBeTruthy();
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeFalsy();
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeTruthy();
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
expect(comparator.lessThan('b', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeTruthy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();

comparator.reverse();

expect(comparator.equal('a', 'b')).toBeTruthy();
expect(comparator.equal('a', '')).toBeFalsy();
expect(comparator.lessThen('b', 'aa')).toBeFalsy();
expect(comparator.greaterThenOrEqual('a', 'aa')).toBeTruthy();
expect(comparator.greaterThenOrEqual('aa', 'a')).toBeFalsy();
expect(comparator.greaterThenOrEqual('a', 'a')).toBeTruthy();
expect(comparator.lessThan('b', 'aa')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'aa')).toBeTruthy();
expect(comparator.greaterThanOrEqual('aa', 'a')).toBeFalsy();
expect(comparator.greaterThanOrEqual('a', 'a')).toBeTruthy();
});
});

0 comments on commit 297875e

Please sign in to comment.