Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5638e66

Browse files
Merge branch 'master' into improve-bubble-sort
2 parents d0ed0af + c8b3fb9 commit 5638e66

File tree

13 files changed

+259
-45
lines changed

13 files changed

+259
-45
lines changed

‎README.md

Lines changed: 3 additions & 1 deletion
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

Lines changed: 212 additions & 0 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class BubbleSort extends Sort {
55
// Flag that holds info about whether the swap has occur or not.
66
let swapped = false;
77
// Clone original array to prevent its modification.
8-
const array = originalArray.slice(0);
8+
const array = [...originalArray];
99

1010
for (let i = 1; i < array.length; i += 1) {
1111
swapped = false;
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Sort from '../Sort';
22

33
export default class InsertionSort extends Sort {
44
sort(originalArray) {
5-
const array = originalArray.slice(0);
5+
const array = [...originalArray];
66

77
// Go through all array elements...
88
for (let i = 0; i < array.length; i += 1) {
@@ -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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class QuickSort extends Sort {
44
sort(originalArray) {
55
// Clone original array to prevent it from modification.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
// If array has less then or equal to one elements then it is already sorted.
99
if (array.length <= 1) {
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class SelectionSort extends Sort {
44
sort(originalArray) {
55
// Clone original array to prevent its modification.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
for (let i = 0; i < array.length - 1; i += 1) {
99
let minIndex = i;
@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Sort from '../Sort';
33
export default class ShellSort extends Sort {
44
sort(originalArray) {
55
// Prevent original array from mutations.
6-
const array = originalArray.slice(0);
6+
const array = [...originalArray];
77

88
// Define a gap distance.
99
let gap = Math.floor(array.length / 2);
@@ -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

Lines changed: 1 addition & 1 deletion
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],

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /