| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ Each algorithm and data structure have its own separate README | |||
| 10 | 10 | with related explanations and links for further reading and YouTube | |
| 11 | 11 | videos. | |
| 12 | 12 | ||
| 13 | + Read this in other languages: [Chinese](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md) | ||
| 14 | + | ||
| 13 | 15 | ## Data Structures | |
| 14 | 16 | ||
| 15 | 17 | 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 | |||
| 190 | 192 | | **O(1)** | 1 | 1 | 1 | | |
| 191 | 193 | | **O(log N)** | 3 | 6 | 9 | | |
| 192 | 194 | | **O(N)** | 10 | 100 | 1000 | | |
| 193 | - | **O(N log N)** | 30 | 60 | 9000 | | ||
| 195 | + | **O(N log N)** | 30 | 600 | 9000 | | ||
| 194 | 196 | | **O(N^2)** | 100 | 10000 | 1000000 | | |
| 195 | 197 | | **O(2^N)** | 1024 | 1.26e+29 | 1.07e+301 | | |
| 196 | 198 | | **O(N!)** | 3628800 | 9.3e+157 | 4.02e+2567 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,7 +22,7 @@ export default function binarySearch(sortedArray, seekElement, comparatorCallbac | |||
| 22 | 22 | } | |
| 23 | 23 | ||
| 24 | 24 | // 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)) { | ||
| 26 | 26 | // Go to the right half of the array. | |
| 27 | 27 | startIndex = middleIndex + 1; | |
| 28 | 28 | } else { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,7 @@ export default class BubbleSort extends Sort { | |||
| 5 | 5 | // Flag that holds info about whether the swap has occur or not. | |
| 6 | 6 | let swapped = false; | |
| 7 | 7 | // Clone original array to prevent its modification. | |
| 8 | - const array = originalArray.slice(0); | ||
| 8 | + const array = [...originalArray]; | ||
| 9 | 9 | ||
| 10 | 10 | for (let i = 1; i < array.length; i += 1) { | |
| 11 | 11 | swapped = false; | |
@@ -18,7 +18,7 @@ export default class BubbleSort extends Sort { | |||
| 18 | 18 | this.callbacks.visitingCallback(array[j]); | |
| 19 | 19 | ||
| 20 | 20 | // 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])) { | ||
| 22 | 22 | const tmp = array[j + 1]; | |
| 23 | 23 | array[j + 1] = array[j]; | |
| 24 | 24 | array[j] = tmp; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,7 +2,7 @@ import Sort from '../Sort'; | |||
| 2 | 2 | ||
| 3 | 3 | export default class InsertionSort extends Sort { | |
| 4 | 4 | sort(originalArray) { | |
| 5 | - const array = originalArray.slice(0); | ||
| 5 | + const array = [...originalArray]; | ||
| 6 | 6 | ||
| 7 | 7 | // Go through all array elements... | |
| 8 | 8 | for (let i = 0; i < array.length; i += 1) { | |
@@ -15,7 +15,7 @@ export default class InsertionSort extends Sort { | |||
| 15 | 15 | // If this is the case then swap that elements. | |
| 16 | 16 | while ( | |
| 17 | 17 | array[currentIndex - 1] && | |
| 18 | - this.comparator.lessThen(array[currentIndex], array[currentIndex - 1]) | ||
| 18 | + this.comparator.lessThan(array[currentIndex], array[currentIndex - 1]) | ||
| 19 | 19 | ) { | |
| 20 | 20 | // Call visiting callback. | |
| 21 | 21 | this.callbacks.visitingCallback(array[currentIndex - 1]); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,7 +31,7 @@ export default class MergeSort extends Sort { | |||
| 31 | 31 | let minimumElement = null; | |
| 32 | 32 | ||
| 33 | 33 | // Find minimum element of two arrays. | |
| 34 | - if (this.comparator.lessThenOrEqual(leftArray[0], rightArray[0])) { | ||
| 34 | + if (this.comparator.lessThanOrEqual(leftArray[0], rightArray[0])) { | ||
| 35 | 35 | minimumElement = leftArray.shift(); | |
| 36 | 36 | } else { | |
| 37 | 37 | minimumElement = rightArray.shift(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ import Sort from '../Sort'; | |||
| 3 | 3 | export default class QuickSort extends Sort { | |
| 4 | 4 | sort(originalArray) { | |
| 5 | 5 | // Clone original array to prevent it from modification. | |
| 6 | - const array = originalArray.slice(0); | ||
| 6 | + const array = [...originalArray]; | ||
| 7 | 7 | ||
| 8 | 8 | // If array has less then or equal to one elements then it is already sorted. | |
| 9 | 9 | if (array.length <= 1) { | |
@@ -27,7 +27,7 @@ export default class QuickSort extends Sort { | |||
| 27 | 27 | ||
| 28 | 28 | if (this.comparator.equal(currentElement, pivotElement)) { | |
| 29 | 29 | centerArray.push(currentElement); | |
| 30 | - } else if (this.comparator.lessThen(currentElement, pivotElement)) { | ||
| 30 | + } else if (this.comparator.lessThan(currentElement, pivotElement)) { | ||
| 31 | 31 | leftArray.push(currentElement); | |
| 32 | 32 | } else { | |
| 33 | 33 | rightArray.push(currentElement); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ import Sort from '../Sort'; | |||
| 3 | 3 | export default class SelectionSort extends Sort { | |
| 4 | 4 | sort(originalArray) { | |
| 5 | 5 | // Clone original array to prevent its modification. | |
| 6 | - const array = originalArray.slice(0); | ||
| 6 | + const array = [...originalArray]; | ||
| 7 | 7 | ||
| 8 | 8 | for (let i = 0; i < array.length - 1; i += 1) { | |
| 9 | 9 | let minIndex = i; | |
@@ -16,7 +16,7 @@ export default class SelectionSort extends Sort { | |||
| 16 | 16 | // Call visiting callback. | |
| 17 | 17 | this.callbacks.visitingCallback(array[j]); | |
| 18 | 18 | ||
| 19 | - if (this.comparator.lessThen(array[j], array[minIndex])) { | ||
| 19 | + if (this.comparator.lessThan(array[j], array[minIndex])) { | ||
| 20 | 20 | minIndex = j; | |
| 21 | 21 | } | |
| 22 | 22 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,7 +3,7 @@ import Sort from '../Sort'; | |||
| 3 | 3 | export default class ShellSort extends Sort { | |
| 4 | 4 | sort(originalArray) { | |
| 5 | 5 | // Prevent original array from mutations. | |
| 6 | - const array = originalArray.slice(0); | ||
| 6 | + const array = [...originalArray]; | ||
| 7 | 7 | ||
| 8 | 8 | // Define a gap distance. | |
| 9 | 9 | let gap = Math.floor(array.length / 2); | |
@@ -20,7 +20,7 @@ export default class ShellSort extends Sort { | |||
| 20 | 20 | this.callbacks.visitingCallback(array[currentIndex]); | |
| 21 | 21 | ||
| 22 | 22 | // 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])) { | ||
| 24 | 24 | const tmp = array[currentIndex]; | |
| 25 | 25 | array[currentIndex] = array[gapShiftedIndex]; | |
| 26 | 26 | array[gapShiftedIndex] = tmp; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,7 @@ | |||
| 4 | 4 | * @return {number[][]} | |
| 5 | 5 | */ | |
| 6 | 6 | 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). | ||
| 8 | 8 | const possibleMoves = [ | |
| 9 | 9 | [position[0] - 1, position[1] - 2], | |
| 10 | 10 | [position[0] - 2, position[1] - 1], | |
| Back | FazBrowse Home | New Git URL |
0 commit comments