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 4de156e

Browse files
authored
Merge pull request #14 from trekhleb/master
BubbleSort: use Destructuring assignment to swap values
2 parents a92118e + fac2d1f commit 4de156e

File tree

5 files changed

+25
-20
lines changed

5 files changed

+25
-20
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ export default class BubbleSort extends Sort {
1919

2020
// Swap elements if they are in wrong order.
2121
if (this.comparator.lessThan(array[j + 1], array[j])) {
22-
const tmp = array[j + 1];
23-
array[j + 1] = array[j];
24-
array[j] = tmp;
22+
[array[j], array[j + 1]] = [array[j + 1], array[j]];
2523

2624
// Register the swap.
2725
swapped = true;

‎src/algorithms/sorting/counting-sort/__test__/CountingSort.test.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ describe('CountingSort', () => {
2727
const sorter = new CountingSort({ visitingCallback });
2828

2929
// Detect biggest number in array in prior.
30-
const biggestElement = notSortedArr.reduce((accumulator, element) => {
31-
return element > accumulator ? element : accumulator;
32-
}, 0);
30+
const biggestElement = Math.max(...notSortedArr);
3331

3432
// Detect smallest number in array in prior.
35-
const smallestElement = notSortedArr.reduce((accumulator, element) => {
36-
return element < accumulator ? element : accumulator;
37-
}, 0);
33+
const smallestElement = Math.min(...notSortedArr);
3834

3935
const sortedArray = sorter.sort(notSortedArr, smallestElement, biggestElement);
4036

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ export default class SelectionSort extends Sort {
2323

2424
// If new minimum element has been found then swap it with current i-th element.
2525
if (minIndex !== i) {
26-
const tmp = array[i];
27-
array[i] = array[minIndex];
28-
array[minIndex] = tmp;
26+
[array[i], array[minIndex]] = [array[minIndex], array[i]];
2927
}
3028
}
3129

‎src/data-structures/linked-list/README.zh-CN.md‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,20 @@ Add(value)
2828
end if
2929
end Add
3030
```
31-
31+
32+
```
33+
Prepend(value)
34+
Pre: value is the value to add to the list
35+
Post: value has been placed at the head of the list
36+
n ← node(value)
37+
n.next ← head
38+
head ← n
39+
if tail = ø
40+
tail ← n
41+
end
42+
end Prepend
43+
```
44+
3245
### 搜索
3346

3447
```text
@@ -67,10 +80,10 @@ Remove(head, value)
6780
end if
6881
return true
6982
end if
70-
while n.next = ø and n.next.value = value
83+
while n.next != ø and n.next.value != value
7184
n ← n.next
7285
end while
73-
if n.next = ø
86+
if n.next != ø
7487
if n.next = tail
7588
tail ← n
7689
end if
@@ -88,7 +101,7 @@ Traverse(head)
88101
Pre: head is the head node in the list
89102
Post: the items in the list have been traversed
90103
n ← head
91-
while n = 0
104+
while n != 0
92105
yield n.value
93106
n ← n.next
94107
end while
@@ -101,11 +114,11 @@ end Traverse
101114
ReverseTraversal(head, tail)
102115
Pre: head and tail belong to the same list
103116
Post: the items in the list have been traversed in reverse order
104-
if tail = ø
117+
if tail != ø
105118
curr ← tail
106-
while curr = head
119+
while curr != head
107120
prev ← head
108-
while prev.next = curr
121+
while prev.next != curr
109122
prev ← prev.next
110123
end while
111124
yield curr.value

‎src/data-structures/tree/fenwick-tree/FenwickTree.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default class FenwickTree {
6060
*/
6161
queryRange(leftIndex, rightIndex) {
6262
if (leftIndex > rightIndex) {
63-
throw new Error('Left index can not be greater then right one');
63+
throw new Error('Left index can not be greater than right one');
6464
}
6565

6666
if (leftIndex === 1) {

0 commit comments

Comments
(0)

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