8000 Update merge (#1) · tstillwell/javascript-algorithms@d3ff2b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3ff2b0

Browse files
authored
Update merge (#1)
* chore: ignore .DS_Store in git (trekhleb#65) * Fix BST removal method. * Simplify combineWithoutRepetition algorithm. * add recursive solution to permutations with repetitions problem (trekhleb#52) * add recursive solution to permutations with repetitions problem * fix untested function, failing test * add comments * Add recursive way of generating permutations with repetitions. * Code style fixes. * Update javascript-algorithms-and-data-structures version. * fix unbound knapsack problem with items more than 1(default value) (trekhleb#73) * Code style fixes. * Correct a comment (trekhleb#66) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Add algorithms complexity to README. * Update README. * Z algorithm implementation (trekhleb#77) * Implemented Z algorithm * Fixed bugs in implementation and added tests * Added README explaining z algorithm * Code style fixes. * Add comments. * Add comments. * Fix BST removal method (trekhleb#74) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Add setValue and nodeCopy methods to binary tree node. * Corrected explanations and included an example (trekhleb#75) * Update README for integer partition. * Added Complexity of Each Algorithm in Sorting/ directory. (trekhleb#76) * Added Complexity to Bubble Sort README.md * Added Complexity to Counting-Sort README.md * Italicized n in Bubble Sort README.md * Added Complexity to Heap Sort README.md * Added Complexity to Insertion Sort README.md * Added Complexity to Merge Sort README.md * Added Complexity to Quick Sort README.md * Added Complexity to Radix Sort README.md * Added Complexity to Selection Sort README.md * Added Complexity to SHell Sort README.md * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Update READMEs. * Add Levenshtein Distance algorithm explanations. * Fix the findEdge method of the graph (trekhleb#80) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph * Code style fix. * Add regular expression matching algorithm. * Fix the value returned by DisjointSet union (trekhleb#81) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph * Fix the value returned by DisjointSet union * Add bit manipulation section. * Add more bit manipulation functions. * Add more bit manipulation functions. * Fix typo. * Simplify combineWithoutRepetitions function. * Simplify combineWithRepetitions function. * Add recursive factorial function (trekhleb#85) * Fix LinkedList * Fix the prepend method for the LinkedList * Fix the remove method for the MinHeap * Correct a comment * Fix BST removal method * Fix the findEdge method of the graph * Fix the value returned by DisjointSet union * Add recursive factorial function * Simplify permutateWithRepetitions algorithm.
1 parent ebfd527 commit d3ff2b0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1432
-447
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
.idea
33
coverage
44
.vscode
5+
.DS_Store

README.md

Lines changed: 118 additions & 110 deletions
Large diffs are not rendered by default.

README.zh-CN.md

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

README.zh-TW.md

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

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/algorithms/math/bits/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Bit Manipulation
2+
3+
#### Get Bit
4+
5+
This method shifts `1` over by `bitPosition` bits, creating a
6+
value that looks like `00100`. Then we perform `AND` operation
7+
that clears all bits from the original number except the
8+
`bitPosition` one. Then we compare the result with zero. If
9+
result is zero that would mean that original number has `0` at
10+
position `bitPosition`.
11+
12+
> See `getBit` function for further details.
13+
14+
#### Set Bit
15+
16+
This method shifts `1` over by `bitPosition` bits, creating a
17+
value that looks like `00100`. Then we perform `OR` operation
18+
that sets specific bit into `1` but it does not affect on
19+
other bits of the number.
20+
21+
> See `setBit` function for further details.
22+
23+
#### Clear Bit
24+
25+
This method shifts `1` over by `bitPosition` bits, creating a
26+
value that looks like `00100`. Than it inverts this mask to get
27+
the number that looks like `11011`. Then `AND` operation is
28+
being applied to both the number and the mask. That operation
29+
unsets the bit.
30+
31+
> See `clearBit` function for further details.
32+
33+
#### Update Bit
34+
35+
This method is a combination of "Clear Bit" and "Set Bit" methods.
36+
37+
> See `updateBit` function for further details.
38+
39+
#### Multiply By Two
40+
41+
This method shifts original number by one bit to the left.
42+
Thus all binary number components (powers of two) are being
43+
multiplying by two and thus the number itself is being
44+
multiplied by two.
45+
46+
```
47+
Before the shift
48+
Number: 0b0101 = 5
49+
Powers of two: 0 + 2^2 + 0 + 2^0
50+
51+
After the shift
52+
Number: 0b1010 = 10
53+
Powers of two: 2^3 + 0 + 2^1 + 0
54+
```
55+
56+
> See `multiplyByTwo` function for further details.
57+
58+
#### Divide By Two
59+
60+
This method shifts original number by one bit to the right.
61+
Thus all binary number components (powers of two) are being
62+
divided by two and thus the number itself is being
63+
divided by two without remainder.
64+
65+
```
66+
Before the shift
67+
Number: 0b0101 = 5
68+
Powers of two: 0 + 2^2 + 0 + 2^0
69+
70+
After the shift
71+
Number: 0b0010 = 2
72+
Powers of two: 0 + 0 + 2^1 + 0
73+
```
74+
75+
> See `divideByTwo` function for further details.
76+
77+
#### Switch Sign
78+
79+
This method make positive numbers to be negative and backwards.
80+
To do so it uses "Twos Complement" approach which does it by
81+
inverting all of the bits of the number and adding 1 to it.
82+
83+
```
84+
1101 -3
85+
1110 -2
86+
1111 -1
87+
0000 0
88+
0001 1
89+
0010 2
90+
0011 3
91+
```
92+
93+
> See `switchSign` function for further details.
94+
95+
## References
96+
97+
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
98+
- [Negative Numbers in binary on YouTube](https://www.youtube.com/watch?v=4qH4unVtJkE&t=0s&index=30&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
99+
- [Bit Hacks on stanford.edu](https://graphics.stanford.edu/~seander/bithacks.html)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import clearBit from '../clearBit';
2+
3+
describe('clearBit', () => {
4+
it('should clear bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(clearBit(1, 0)).toBe(0);
7+
expect(clearBit(1, 1)).toBe(1);
8+
expect(clearBit(1, 2)).toBe(1);
9+
10+
// 10 = 0b1010
11+
expect(clearBit(10, 0)).toBe(10);
12+
expect(clearBit(10, 1)).toBe(8);
13+
expect(clearBit(10, 3)).toBe(2);
14+
});
15+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import divideByTwo from '../divideByTwo';
2+
3+
describe('divideByTwo', () => {
4+
it('should divide numbers by two using bitwise operations', () => {
5+
expect(divideByTwo(0)).toBe(0);
6+
expect(divideByTwo(1)).toBe(0);
7+
expect(divideByTwo(3)).toBe(1);
8+
expect(divideByTwo(10)).toBe(5);
9+
expect(divideByTwo(17)).toBe(8);
10+
expect(divideByTwo(125)).toBe(62);
11+
});
12+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import getBit from '../getBit';
2+
3+
describe('getBit', () => {
4+
it('should get bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(getBit(1, 0)).toBe(1);
7+
expect(getBit(1, 1)).toBe(0);
8+
9+
// 2 = 0b0010
10+
expect(getBit(2, 0)).toBe(0);
11+
expect(getBit(2, 1)).toBe(1);
12+
13+
// 3 = 0b0011
14+
expect(getBit(3, 0)).toBe(1);
15+
expect(getBit(3, 1)).toBe(1);
16+
17+
// 10 = 0b1010
18+
expect(getBit(10, 0)).toBe(0);
19+
expect(getBit(10, 1)).toBe(1);
20+
expect(getBit(10, 2)).toBe(0);
21+
expect(getBit(10, 3)).toBe(1);
22+
});
23+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import multiplyByTwo from '../multiplyByTwo';
2+
3+
describe('multiplyByTwo', () => {
4+
it('should multiply numbers by two using bitwise operations', () => {
5+
expect(multiplyByTwo(0)).toBe(0);
6+
expect(multiplyByTwo(1)).toBe(2);
7+
expect(multiplyByTwo(3)).toBe(6);
8+
expect(multiplyByTwo(10)).toBe(20);
9+
expect(multiplyByTwo(17)).toBe(34);
10+
expect(multiplyByTwo(125)).toBe(250);
11+
});
12+
});

0 commit comments

Comments
 (0)
0